<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Tyler Clemons &#187; Ruby</title>
	<atom:link href="http://www.tylerclemons.com/category/programming/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.tylerclemons.com</link>
	<description>tylerclemons.com</description>
	<lastBuildDate>Wed, 26 May 2010 18:39:58 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Weka, Ruby, Association Mining</title>
		<link>http://www.tylerclemons.com/weka-ruby-association-mining/</link>
		<comments>http://www.tylerclemons.com/weka-ruby-association-mining/#comments</comments>
		<pubDate>Sat, 15 May 2010 21:09:17 +0000</pubDate>
		<dc:creator>Tyler</dc:creator>
				<category><![CDATA[Data Mining]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Weka]]></category>
		<category><![CDATA[Association Mining]]></category>
		<category><![CDATA[rjb]]></category>

		<guid isPermaLink="false">http://www.tylerclemons.com/?p=282</guid>
		<description><![CDATA[Weka is a collection of machine learning tools used for data mining.  Weka is written in Java however it is possible to use Weka&#8217;s libraries inside Ruby.  To do this, we must install the Java, Rjb, and of course obtain the Weka source code.  In this example, I look at Associating Mining.

Refer to my previous [...]]]></description>
			<content:encoded><![CDATA[<p><a title="Weka" href="http://www.cs.waikato.ac.nz/ml/weka/" target="_blank">Weka</a> is a collection of machine learning tools used for data mining.  Weka is written in Java however it is possible to use Weka&#8217;s libraries inside Ruby.  To do this, we must install the Java, Rjb, and of course obtain the Weka source code.  In this example, I look at Associating Mining.</p>
<p><span id="more-282"></span></p>
<p><img title="More..." src="http://tylershome.nfshost.com/home/public/wp-includes/js/tinymce/plugins/wordpress/img/trans.gif" alt="" /><a title="Kmeans and setup" href="http://www.tylerclemons.com/weka-and-ruby/">Refer to my previous example for setup instructions.</a></p>
<p><img title="More..." src="http://tylershome.nfshost.com/home/public/wp-includes/js/tinymce/plugins/wordpress/img/trans.gif" alt="" /><strong>Running Weka</strong></p>
<p>The following is an example of a frequent itemset finder.</p>
<div id="codebox" style="overflow: auto; "><code><br />
require <span style="color: #ffcc00;">'rjb'</span></code></p>
<div style="overflow: auto; "><code><span style="color: #ffcc00;"> </span></code><span style="color: #ff0000;">#&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</span><br />
<span style="color: #800080;">def </span>asscm()</div>
<p style="padding-left: 30px;"><span style="color: #ff0000;">#Load Java Jar</span><br />
dir =<span style="color: #ffcc00;"> &#8220;./weka.jar&#8221;</span><br />
<span style="color: #ff0000;">#Have Rjb load the jar file, and pass Java command line arguments</span><br />
<span style="color: #008000;">Rjb</span>::load(dir, jvmargs=[<span style="color: #ffcc00;">"-Xmx1000M"</span>])</p>
<p style="padding-left: 30px;"><span style="color: #ff0000;">#make k-means classifier</span><br />
obj = <span style="color: #008000;">Rjb</span>::import(<span style="color: #ffcc00;">&#8220;weka.associations.Apriori&#8221;</span>)<br />
assc= obj.new</p>
<p style="padding-left: 30px;"><span style="color: #ff0000;">#load the data</span> <span style="color: #ff0000;">using Ja<span style="color: #ff0000;">va</span></span><span style="color: #ff0000;"> and Weka</span><br />
weather_src = Rjb::import(<span style="color: #ffcc00;">&#8220;java.io.FileReader&#8221;</span>).new(<span style="color: #ffcc00;">&#8220;weather.nominal.arff&#8221;</span>)<br />
weather_data = Rjb::import(<span style="color: #ffcc00;">&#8220;weka.core.Instances&#8221;</span>).new(weather_src)</p>
<p style="padding-left: 30px;"><span style="color: #ff0000;">#Find the frequent itemsets</span><br />
assc.setCar(true) <span style="color: #ff0000;">#mines for class association</span><br />
assc.setLowerBoundMinSupport(0.25) <span style="color: #ff0000;">#set minimum support</span><br />
assc.buildAssociations(weather_data)<br />
puts assc.toString</p>
<p><span style="color: #800080;">end</span></p>
<p><span style="color: #ff0000;">#&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</span><br />
asscm()</div>
<p>We first tell Rjb to load the specified classpath, for us it&#8217;s our Jar file.  I passed command line arguments that specify the amount of RAM to use.</p>
<p>Rjb::import loads specific classes.  These are relative to our classpath.</p>
<p>I call the constructor for the new classes by using the .new method from Ruby.  Afterward, I can use the new object like any other Ruby object.  The method names are as they are found in their Java source files.  For an explanation of data type conversions, <a title="Rjb explanation" href="http://rjb.rubyforge.org/">click here.</a></p>
<p>The dataset I used is different from the previous because we can&#8217;t use numerical values.  The dataset is found inside the data folder downloaded with weka.jar.</p>
<p>As I said before, this can be done in JRuby.  A great example can be found at this <a title="JRuby Example" href="http://rubyforscientificresearch.blogspot.com/2009/08/accessing-weka-from-jruby.html" target="_blank">great blog post</a>, which inspired my post.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tylerclemons.com/weka-ruby-association-mining/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Weka, Ruby, Decision Trees</title>
		<link>http://www.tylerclemons.com/weka-ruby-decision-trees/</link>
		<comments>http://www.tylerclemons.com/weka-ruby-decision-trees/#comments</comments>
		<pubDate>Sat, 15 May 2010 20:40:26 +0000</pubDate>
		<dc:creator>Tyler</dc:creator>
				<category><![CDATA[Data Mining]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Weka]]></category>
		<category><![CDATA[Decision Trees]]></category>
		<category><![CDATA[rjb]]></category>

		<guid isPermaLink="false">http://www.tylerclemons.com/?p=276</guid>
		<description><![CDATA[Weka is a collection of machine learning tools used for data mining.  Weka is written in Java however it is possible to use Weka&#8217;s libraries inside Ruby.  To do this, we must install the Java, Rjb, and of course obtain the Weka source code.  In this example, I look at Decision Trees.
Refer to my previous [...]]]></description>
			<content:encoded><![CDATA[<p><a title="Weka" href="http://www.cs.waikato.ac.nz/ml/weka/" target="_blank">Weka</a> is a collection of machine learning tools used for data mining.  Weka is written in Java however it is possible to use Weka&#8217;s libraries inside Ruby.  To do this, we must install the Java, Rjb, and of course obtain the Weka source code.  In this example, I look at Decision Trees.</p>
<p><span id="more-276"></span><a title="Kmeans and setup" href="http://www.tylerclemons.com/weka-and-ruby/">Refer to my previous example for setup instructions.</a></p>
<p><img title="More..." src="http://tylershome.nfshost.com/home/public/wp-includes/js/tinymce/plugins/wordpress/img/trans.gif" alt="" /><strong>Running Weka</strong></p>
<p>The following is an example of running a Decision Tree.</p>
<div id="codebox" style="overflow: auto; "><code><br />
require <span style="color: #ffcc00;">'rjb'</span></code></p>
<div style="overflow: auto; "><code><span style="color: #ffcc00;"> </span></code><span style="color: #ff0000;">#&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</span><br />
<span style="color: #800080;">def </span>dtree()</div>
<p style="padding-left: 30px;"><span style="color: #ff0000;">#Load Java Jar</span><br />
dir =<span style="color: #ffcc00;"> &#8220;./weka.jar&#8221;</span><br />
<span style="color: #ff0000;">#Have Rjb load the jar file, and pass Java command line arguments</span><br />
<span style="color: #008000;">Rjb</span>::load(dir, jvmargs=[<span style="color: #ffcc00;">"-Xmx1000M"</span>])</p>
<p style="padding-left: 30px;"><span style="color: #ff0000;">#make k-means classifier</span><br />
obj = <span style="color: #008000;">Rjb</span>::import(<span style="color: #ffcc00;">&#8220;weka.classifiers.trees.J48&#8243;</span>)<br />
dtree = obj.new</p>
<p style="padding-left: 30px;"><span style="color: #ff0000;">#load the data</span> <span style="color: #ff0000;">using Ja<span style="color: #ff0000;">va</span></span><span style="color: #ff0000;"> and Weka</span><br />
labor_src = Rjb::import(<span style="color: #ffcc00;">&#8220;java.io.FileReader&#8221;</span>).new(<span style="color: #ffcc00;">&#8220;labor.arff&#8221;</span>)<br />
labor_data = Rjb::import(<span style="color: #ffcc00;">&#8220;weka.core.Instances&#8221;</span>).new(labor_src)</p>
<p style="padding-left: 30px;"><span style="color: #ff0000;">#set the class attribute, here it&#8217;s the last value, and then build the classifier</span><br />
labor_data.setClassIndex(labor_data.numAttributes() &#8211; 1)<br />
dtree.buildClassifier(labor_data)<br />
puts dtree.toString</p>
<p style="padding-left: 30px;"><span style="color: #ff0000;">#examine the particular datapoints</span><br />
points = labor_data.numInstances<br />
points.times {|instance|</p>
<p style="padding-left: 60px;">theclass = dtree.classifyInstance(labor_data.instance(instance))<br />
point = labor_data.instance(instance).toString<br />
puts <span style="color: #ff6600;">&#8220;#{point} \t #{theclass}&#8221;</span></p>
<p style="padding-left: 30px;">}</p>
<p><span style="color: #800080;">end</span></p>
<p><span style="color: #ff0000;">#&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</span><br />
dtree()</div>
<p>We first tell Rjb to load the specified classpath, for us it&#8217;s our Jar file.  I passed command line arguments that specify the amount of RAM to use.</p>
<p>Rjb::import loads specific classes.  These are relative to our classpath.</p>
<p>I call the constructor for the new classes by using the .new method from Ruby.  Afterward, I can use the new object like any other Ruby object.  The method names are as they are found in their Java source files.  For an explanation of data type conversions, <a title="Rjb explanation" href="http://rjb.rubyforge.org/">click here.</a></p>
<p>The dataset is found inside the data folder downloaded with weka.jar.</p>
<p>As I said before, this can be done in JRuby.  A great example can be found at this <a title="JRuby Example" href="http://rubyforscientificresearch.blogspot.com/2009/08/accessing-weka-from-jruby.html" target="_blank">great blog post</a>, which inspired my post.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tylerclemons.com/weka-ruby-decision-trees/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Weka and Ruby</title>
		<link>http://www.tylerclemons.com/weka-and-ruby/</link>
		<comments>http://www.tylerclemons.com/weka-and-ruby/#comments</comments>
		<pubDate>Thu, 06 May 2010 17:21:15 +0000</pubDate>
		<dc:creator>Tyler</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Data Mining]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Weka]]></category>
		<category><![CDATA[rjb]]></category>
		<category><![CDATA[K-Means]]></category>

		<guid isPermaLink="false">http://www.tylerclemons.com/?p=256</guid>
		<description><![CDATA[Weka is a collection of machine learning tools used for data mining.  Weka is written in Java however it is possible to use Weka&#8217;s libraries inside Ruby.  To do this, we must install the Java, Rjb, and of course obtain the Weka source code.  One could use JRuby, but I wanted to try this method [...]]]></description>
			<content:encoded><![CDATA[<p><a title="Weka" href="http://www.cs.waikato.ac.nz/ml/weka/" target="_blank">Weka</a> is a collection of machine learning tools used for data mining.  Weka is written in Java however it is possible to use Weka&#8217;s libraries inside Ruby.  To do this, we must install the Java, Rjb, and of course obtain the Weka source code.  One could use JRuby, but I wanted to try this method to eliminate the dependency with JRuby.</p>
<p><span id="more-256"></span><strong>Setup</strong></p>
<p>The first step is to install Java.  That&#8217;s very simple, just follow <a title="Java" href="http://java.sun.com/javase/downloads/index.jsp">this link</a> to download Java.  be sure to download the JDK.</p>
<p>Getting Rjb, which stands for Ruby Java Bridge, is a ruby-gem.  You can either install the gem using the command line:</p>
<div id="codebox" style="overflow: auto; "><code><br />
gem install rjb<br />
</code></div>
<p>or, we can download the gem from <a title="Rjb" href="http://rubyforge.org/frs/?group_id=2010&amp;release_id=6285">this link.</a> The readme inside the gem is very easy to follow.<a title="Rjb" href="http://rubyforge.org/frs/?group_id=2010&amp;release_id=6285"><br />
</a></p>
<p>The last step in setup is obtaining Weka.  On the download page of Weka, choose &#8220;<strong>Other platforms (Linux, etc.)</strong>&#8220;  Inside, we find a jar file called &#8220;weka.jar&#8221;  This is what we need to continue. <strong> </strong></p>
<p><strong>Running Weka</strong></p>
<p>The following is an example of running the SimpleKmeans classifier.</p>
<div id="codebox" style="overflow: auto; "><code><br />
require <span style="color: #ffcc00;">'rjb'</span></code></p>
<p><span style="color: #ff0000;">#&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</span><br />
<span style="color: #800080;">def </span>kmeans()</p>
<p style="padding-left: 30px;"><span style="color: #ff0000;">#Load Java Jar</span><br />
dir =<span style="color: #ffcc00;"> &#8220;./weka.jar&#8221;</span><br />
<span style="color: #ff0000;">#Have Rjb load the jar file, and pass Java command line arguments</span><br />
<span style="color: #008000;">Rjb</span>::load(dir, jvmargs=[<span style="color: #ffcc00;">"-Xmx1000M"</span>])</p>
<p style="padding-left: 30px;"><span style="color: #ff0000;">#make k-means classifier</span><br />
obj = <span style="color: #008000;">Rjb</span>::import(<span style="color: #ffcc00;">&#8220;weka.clusterers.SimpleKMeans&#8221;</span>)<br />
kmeans = obj.new</p>
<p style="padding-left: 30px;"><span style="color: #ff0000;">#load the data</span> <span style="color: #ff0000;">using Ja<span style="color: #ff0000;">va</span></span><span style="color: #ff0000;"> and Weka</span><br />
labor_src = Rjb::import(<span style="color: #ffcc00;">&#8220;java.io.FileReader&#8221;</span>).new(<span style="color: #ffcc00;">&#8220;labor.arff&#8221;</span>)<br />
labor_data = Rjb::import(<span style="color: #ffcc00;">&#8220;weka.core.Instances&#8221;</span>).new(labor_src)</p>
<p style="padding-left: 30px;"><span style="color: #ff0000;">#build the cluster and output the k-means data</span><br />
kmeans.buildClusterer(labor_data)<br />
puts kmeans.toString</p>
<p style="padding-left: 30px;"><span style="color: #ff0000;">#examine the particular datapoints</span><br />
points = labor_data.numInstances<br />
points.times {|instance|</p>
<p style="padding-left: 60px;">cluster = kmeans.clusterInstance(labor_data.instance(instance))<br />
point = labor_data.instance(instance).toString<br />
puts <span style="color: #ff6600;">&#8220;#{point} \t #{cluster}&#8221;</span></p>
<p style="padding-left: 30px;">}</p>
<p><span style="color: #800080;">end</span></p>
<p><span style="color: #ff0000;">#&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</span><br />
kmeans()</div>
<p>This is a simple example of how to use K-means.  We first tell Rjb to load the specified classpath, for us it&#8217;s our Jar file.  I passed command line arguments that specify the amount of RAM to use.</p>
<p>Rjb::import loads specific classes.  These are relative to our classpath.</p>
<p>I call the constructor for the new classes by using the .new method from Ruby.  Afterward, I can use the new object like any other Ruby object.  The method names are as they are found in their Java source files.  For an explanation of data type conversions, <a title="Rjb explanation" href="http://rjb.rubyforge.org/">click here.</a></p>
<p>The dataset is found inside the data folder downloaded with weka.jar.</p>
<p>I will probably look at the other classifiers and post some more example.  As I said before, this can be done in JRuby.  A great example can be found at this <a title="JRuby Example" href="http://rubyforscientificresearch.blogspot.com/2009/08/accessing-weka-from-jruby.html" target="_blank">great blog post</a>, which inspired my post.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tylerclemons.com/weka-and-ruby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Finding with Ruby Rails</title>
		<link>http://www.tylerclemons.com/finding-with-ruby-rails/</link>
		<comments>http://www.tylerclemons.com/finding-with-ruby-rails/#comments</comments>
		<pubDate>Sat, 28 Jun 2008 21:14:59 +0000</pubDate>
		<dc:creator>Tyler</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://tylershome.nfshost.com/?p=33</guid>
		<description><![CDATA[I&#8217;m sure anyone that has spent enough time with Rails has found this little annoyance.  The basic find() is pretty simple.

result = Table.find(params[:id])

A select query is performed on Table that seeks the row whose primary key matches the value returned by params[:id] Pretty cool, until it fails  

The above code fails and raises an [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m sure anyone that has spent enough time with Rails has found this little annoyance.  The basic find() is pretty simple.</p>
<div id="codebox" style="overflow: auto; ">
<p><em><span style="color: #ff9900;"><span style="color: #000000;"><span style="color: #ffffff;">result</span> <span style="color: #888888;">= </span></span>Table</span>.find(params[:id])</em><em><br />
</em></div>
<p>A select query is performed on Table that seeks the row whose primary key matches the value returned by params[:id] Pretty cool, until it fails <img src='http://tylershome.nfshost.com/home/public/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
<p><span id="more-14"></span></p>
<p>The above code fails and raises an error if no records are found.  Handling raised errors is a little bit of pain compared to a couple of if-then-else statements.  A better way to find is to use a variation of the following syntax:</p>
<div id="codebox" style="overflow: auto; ">
<p><em><span style="color: #ff9900;"><span style="color: #000000;"><span style="color: #ffffff;">result</span> <span style="color: #888888;">=</span></span> Table</span>.find<span style="color: #99cc00;"><span style="color: #008000;">(:</span><span style="color: #008000;">first</span></span>,<span style="color: #99cc00;"><span style="color: #008000;">:</span><span style="color: #008000;">conditions</span></span>=&gt; ["id=?",params[:id]])</em></div>
<p>Instead of throwing a nasty error, result will become <em>nil</em> if no records are found.  Then it becomes possible to check if the query was successful by checking if result is not <em>nil</em> instead of catching errors.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tylerclemons.com/finding-with-ruby-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>OS Independent Excel Reports</title>
		<link>http://www.tylerclemons.com/os-independent-excel-reports/</link>
		<comments>http://www.tylerclemons.com/os-independent-excel-reports/#comments</comments>
		<pubDate>Sat, 28 Jun 2008 20:18:49 +0000</pubDate>
		<dc:creator>Tyler</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[win32]]></category>
		<category><![CDATA[Add new tag]]></category>
		<category><![CDATA[Excel]]></category>
		<category><![CDATA[spreadsheet]]></category>

		<guid isPermaLink="false">http://tylershome.nfshost.com/?p=32</guid>
		<description><![CDATA[I ran into this problem awhile ago. I was tasked with making Excel reports using Ruby on Rails. Since I was running on Windows, I just used Win32ole. It worked quite well until I remembered that Win32 is not platform independent.  Basically, if the system is hosted on a non-Windows machine the Excel Report will [...]]]></description>
			<content:encoded><![CDATA[<p>I ran into this problem awhile ago. I was tasked with making Excel reports using Ruby on Rails. Since I was running on Windows, I just used Win32ole. It worked quite well until I remembered that Win32 is not platform independent.  Basically, if the system is hosted on a non-Windows machine the Excel Report will fail.  Great.  But, Ruby isn&#8217;t out for the count yet.  I found a pretty cool spreadsheet generator at RubyForge.  After installing spreadsheet, it is pretty simple to use.  You can either install it by downloading it from the web, click <a href="http://rubyforge.org/projects/spreadsheet/" target="_blank">HERE</a>, or use <a href="http://rubyforge.org/frs/?group_id=126" target="_blank">ruby gems</a> by typing this on the command line: &#8220;gem install spreadsheet-excel&#8221;</p>
<p><span id="more-13"></span></p>
<div id="codebox" style="overflow: auto; "><em>require &#8220;spreadsheet/excel&#8221;</em></p>
<p><em><span style="color: #ff0000;">#create the workbook</span><br />
workbook = <span style="color: #00ff00;">Spreadsheet</span>::<span style="color: #00ff00;">Excel</span>.new(&#8221;reports/file.xls&#8221;)</em></p>
<p><em><span style="color: #ff0000;">#make some formats</span><br />
headers = <span style="color: #00ff00;">Format</span>.new(<span style="color: #339966;">:color</span> =&gt; &#8220;black&#8221;,<span style="color: #339966;"> :bold</span> =&gt;<span style="color: #3366ff;"> true</span>)</em></p>
<p><em>data = <span style="color: #00ff00;">Format</span>.new(<span style="color: #339966;">:color</span> =&gt; &#8220;black&#8221;, <span style="color: #339966;">:bold</span> =&gt; <span style="color: #3366ff;">false</span>, <span style="color: #339966;">:align</span> =&gt; &#8220;right&#8221;)</em></p>
<p><span style="color: #ff0000;"><em>#spreadsheet uses numbers for rows and columns i.e. A1 = 00</em></span></p>
<p><em>column = 0<br />
row = 0</em></p>
<p><span style="color: #ff0000;"><em>#write some headers from some header object, assume this is an array</em></span></p>
<p><em>header.each |field|</em></p>
<p style="padding-left: 30px;"><em>worksheet.write(column,row,field.chomp,top)<br />
column+=1</em></p>
<p><em><span style="color: #ff00ff;">end</span></em></p>
<p><em><span style="color: #ff0000;">#now insert data so move down a row</span><br />
column = 0 </em></p>
<p><em>row = 1</em></p>
<p><span style="color: #ff0000;"><em>#the object list is an array<br />
#each entry is a result of the query against the database </em></span></p>
<p><em>object_list.each <span style="color: #ff00ff;">do</span> |object|</em></p>
<p style="padding-left: 30px;"><em>headers.each <span style="color: #ff00ff;">do</span> |value|</em></p>
<p style="padding-left: 60px;"><em>worksheet.write(row,column,object[value],data) </em></p>
<p style="padding-left: 60px;"><em>column +=1</em></p>
<p style="padding-left: 30px;"><em><span style="color: #ff00ff;">end</span><br />
row +=1</em></p>
<p><em><span style="color: #ff00ff;">end</span></em></p>
<p><em>#finished so close<br />
workbook.close</em></div>
<p>Pretty simple. I like the formatting features. It is very easy to switch a format style after defining a particular format object. One of the design decisions that can be argued is how to actually write the data. An alternate approach is to write the headers one at a time, and as each header is written, iterate through the objects that we wish to write. The described method is inefficient because it ignores how Ruby stores its arrays, row-major order.</p>
<p>An alternate approach to calling the write method is to pass an array.  Unfortunately, when objects are returned by using the find method, the fields do not return in a specific order so there is no guarantee the headers we wrote at the top of the spreadsheet will be in the same order as the objects that were returned. I deployed a version of this with Ruby Rails.</p>
<p>In my own experience, my new function had surpassed my old Win32 based function in both speed and portability thanks to spreadsheet. I recommend using this method even if your project is slated for a windows box with office installed.  Unless you need formulas, then you are out of luck <img src='http://tylershome.nfshost.com/home/public/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
<p>The example is almost finished. I encapsulated the above function in a method. You can decide where to implement it, either in one of the helper controllers or in the current controller. In my implementation, a user navigates to the reports page, navigates some Ajax controls and submits, using a button that calls the function below, a desired report. The view for display_printout is left blank.</p>
<div id="codebox" style="overflow: auto; ">
<p><em><span style="color: #ff00ff;">def</span> <span style="color: #0000ff;">display_printout</span></em></p>
<p style="padding-left: 30px;"><em><span style="color: #ff0000;">#I named the excel report create_printout<br />
#I included some error handling and a success returns true</span><br />
<span style="color: #ff00ff;">if</span> create_printout</em></p>
<p style="padding-left: 60px;"><em>send_file &#8220;reports/report.xls&#8221;</em></p>
<p style="padding-left: 30px;"><em><span style="color: #ff00ff;">else</span></em></p>
<p style="padding-left: 60px;"><span style="color: #ff0000;"><em>#display some sort of error message</em></span></p>
<p style="padding-left: 30px;"><em><span style="color: #ff00ff;">end</span></em></p>
<p><em><span style="color: #ff00ff;">end</span></em></div>
]]></content:encoded>
			<wfw:commentRss>http://www.tylerclemons.com/os-independent-excel-reports/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby Speaks</title>
		<link>http://www.tylerclemons.com/ruby-speaks/</link>
		<comments>http://www.tylerclemons.com/ruby-speaks/#comments</comments>
		<pubDate>Sat, 28 Jun 2008 05:28:01 +0000</pubDate>
		<dc:creator>Tyler</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[sapi5]]></category>
		<category><![CDATA[win32]]></category>

		<guid isPermaLink="false">http://tylershome.nfshost.com/?p=31</guid>
		<description><![CDATA[Learned a new trick the other day.  I was searching around and I found out how to make Ruby talk.  So far this only works for Windows machines because the win32 files are only available in Windows&#8230; as far as my knowledge.  Copy and paste the following code into any editor.  [...]]]></description>
			<content:encoded><![CDATA[<p>Learned a new trick the other day.  I was searching around and I found out how to make Ruby talk.  So far this only works for Windows machines because the win32 files are only available in Windows&#8230; as far as my knowledge.  Copy and paste the following code into any editor.  It prompts the user for a file and reads it.</p>
<p><span id="more-12"></span></p>
<div id="codebox" style="overflow: auto; "><span style="color: #ff00ff;">require</span> &#8220;win32/sapi5&#8243;<br />
<span style="color: #ff00ff;">include</span> Win32</p>
<p>print &#8220;Enter Text File:&#8221;<br />
file = gets</p>
<p>voice = SpVoice.new<br />
speech = &#8220;&#8221;</p>
<p><span style="color: #00ff00;">File</span>.open(file.chomp).each {| words|  speech +=words } <span style="color: #ff0000;">#Automatic Close<br />
</span></p>
<p>voice.Speak(speech)<br />
puts</p></div>
<p>Or, you could just do this:</p>
<div id="codebox" style="overflow: auto; "><span style="color: #ff00ff;">require</span> &#8220;win32/sapi5&#8243;<br />
<span style="color: #ff00ff;">include</span> Win32</p>
<p>voice = SpVoice.new<br />
speech = &#8220;Tyler is cool, but Sharece is cooler.&#8221;</p>
<p>voice.Speak(speech)</p></div>
]]></content:encoded>
			<wfw:commentRss>http://www.tylerclemons.com/ruby-speaks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>FTP fun with Ruby</title>
		<link>http://www.tylerclemons.com/ftp-fun-with-ruby/</link>
		<comments>http://www.tylerclemons.com/ftp-fun-with-ruby/#comments</comments>
		<pubDate>Sun, 15 Jun 2008 21:09:23 +0000</pubDate>
		<dc:creator>Tyler</dc:creator>
				<category><![CDATA[FTP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Class]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[Recursion]]></category>
		<category><![CDATA[WordPress themes]]></category>

		<guid isPermaLink="false">http://tylershome.nfshost.com/?p=19</guid>
		<description><![CDATA[WordPress is pretty cool, save the editor.  One of the coolest features is being able to switch the look and feel of my website without doing much extra work.
To do this, I just search Google for WordPress themes and about a dozen websites pop up with hundreds of themes.  Cool.  I download [...]]]></description>
			<content:encoded><![CDATA[<p>WordPress is pretty cool, save the editor.  One of the coolest features is being able to switch the look and feel of my website without doing much extra work.</p>
<p>To do this, I just search <a title="Google" href="http://www.google.com/search?hl=en&amp;safe=off&amp;client=firefox-a&amp;rls=org.mozilla%3Aen-US%3Aofficial&amp;hs=78J&amp;q=Wordpress+themes&amp;btnG=Search">Google</a> for WordPress themes and about a dozen websites pop up with hundreds of themes.  Cool.  I download a couple of them, all free, and look for the easiest way to deploy them.  However, the only way to do that, is to upload all of the files onto my web server via FTP.</p>
<p>No problem, FTP has multiple file commands&#8230; that don&#8217;t exactly work with directories <img src='http://tylershome.nfshost.com/home/public/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' />   The simple solution is to find an FTP client for MacOS X and resort to a drag and drop method.  Of course there exist some programs out there for free like <a title="CyberDuck" href="http://cyberduck.ch/" target="_blank">Cyberduck</a>, or <a href="http://filezilla-project.org/">Filezilla</a> (but I use OS 10.4 so FileZilla is out), but since it&#8217;s the summer time, I decided to make my own.  To be honest, I didn&#8217;t actually look for an FTP program before making my own lol!  I fired up <a title="Ruby" href="http://www.ruby-lang.org/en/" target="_blank">Ruby</a> for a little fun.</p>
<p><span id="more-9"></span>It is a simple enough task to create an FTP instance in Ruby and to login.  The fun part is deciding how to automate the placing of subdirectories because that feature is not supported by traditional FTP.  It can be done iteratively, but that would be a nightmare for bookkeeping.  For that method to work, you might have to employ a queue type structure that stores found directories.  Or you could just use recursion which makes things a little bit simpler.</p>
<p>For those new to the idea of recursion, read about it <a title="Recursion" href="http://en.wikipedia.org/wiki/Recursion_%28computer_science%29" target="_blank">HERE.</a> Basically, we can call a function that opens a remote FTP directory, lets call it directory A, and iterates through it&#8217;s contents.  When a directory, lets call it directory B, is found, instead of storing it away, we call the same function again on B.  After the function operates on B, the stack returns back to the caller, in this case A, and continues through the contents of A.  If any directories are found in A, B, or any subdirectories, the function just makes another recursive call.  Of course, the recursive stack only goes so far but 99.99% of the time the stack won&#8217;t need to get close to the limit.  Judging from the test I ran, Ruby 1.8.6 exhausted at a little over 6200 levels.</p>
<div id="codebox" style="overflow: auto; ">
<p><em><span style="color: #0000ff;">def</span> <span style="color: #ff00ff;">test_depth</span>(x)</em></p>
<p style="padding-left: 30px;"><em>puts x</em></p>
<p style="padding-left: 30px;"><em>test_depth(x+1)</em></p>
<p><em><span style="color: #0000ff;">end</span></em></p>
<p><em>test_depth(1)</em></p>
<p style="text-align: left;">
</div>
<p>Of course the length of the recursive stack depends on the amount of local variables declared.  I found that for every 4 local variables, and every 4 parameters passed into a function, you should expect to sacrifice 85 levels or 85 recursive calls. Of course those metrics only apply to basic data structures like integers and not complex structures like classes.</p>
<p>If you aren&#8217;t familiar with Ruby FTP, click <a title="Ruby FTP Class" href="http://www.ruby-doc.org/stdlib/libdoc/net/ftp/rdoc/classes/Net/FTP.html" target="_blank">HERE.</a> The FTP code, without error handling, some comments, and output statements, looks like:</p>
<div id="codebox" style="overflow: auto; ">
<p><em><span style="color: #0000ff;">def</span> <span style="color: #ff00ff;">send_it</span>()</em></p>
<p style="padding-left: 30px;"><em>#open local directory<br />
<span style="color: #339966;"> Dir</span>.chdir(<span style="color: #ff6600;">@current_directory</span>[0][-1])</em></p>
<p style="padding-left: 30px;"><em>#make a local copy of file list to reduce network calls<br />
remote_list = <span style="color: #ff6600;">@ftp</span>.nlst()</em></p>
<p style="padding-left: 30px;"><em><span style="color: #339966;">Dir</span>.entries(&#8221;.&#8221;).each { |thefile|</em></p>
<p style="padding-left: 60px;"><em>#be sure not to try and send the current and parent directory<br />
<span style="color: #0000ff;"> if</span> thefile != &#8220;.&#8221; <span style="color: #0000ff;">and</span> thefile != &#8220;..&#8221;</em></p>
<p style="padding-left: 60px;"><em>filename = <span style="color: #339966;">File</span>.basename(thefile)<br />
#check if this is a file<br />
<span style="color: #0000ff;"> if</span> <span style="color: #0000ff;">not</span> <span style="color: #339966;">File</span>.directory?(thefile)</em></p>
<p style="padding-left: 90px;"><em><span style="color: #ffcc00;"> <span style="color: #ff6600;">@ftp</span></span>.putbinaryfile(thefile,thefile)</em></p>
<p style="padding-left: 60px;"><em><span style="color: #0000ff;">else</span></em></p>
<p style="padding-left: 90px;"><em><span style="color: #0000ff;">if not</span> remote_list.index(filename)</em></p>
<p style="padding-left: 120px;"><em><span style="color: #ffcc00;"><span style="color: #ff6600;">@ftp</span>.</span>mkdir(filename)</em></p>
<p style="padding-left: 90px;"><em><span style="color: #0000ff;"> end</span></em></p>
<p style="padding-left: 60px;">
<p style="padding-left: 60px;"><em>#change directory and make recursive call<br />
<span style="color: #ffcc00;"> <span style="color: #ff6600;">@ftp</span></span>.chdir(filename)<br />
<span style="color: #ffcc00;"> <span style="color: #ff6600;">@current_directory</span></span>[0] &lt;&lt; filename<br />
<span style="color: #ffcc00;"> <span style="color: #ff6600;">@current_directory</span></span>[1] &lt;&lt; filename<br />
send_it()</em></p>
<p style="padding-left: 60px;"><em>#move onto next file after sending directory<br />
<span style="color: #ffcc00;"> <span style="color: #ff6600;">@ftp</span></span>.chdir(&#8221;..&#8221;)<br />
<span style="color: #ffcc00;"> <span style="color: #ff6600;">@current_directory</span></span>[1].delete_at(-1)<br />
<span style="color: #339966;"> Dir</span>.chdir(&#8221;..&#8221;)<br />
<span style="color: #ffcc00;"> <span style="color: #ff6600;">@current_directory</span></span>[0].delete_at(-1)<br />
<span style="color: #0000ff;"> end</span></em></p>
<p style="padding-left: 60px;"><em><span style="color: #0000ff;"> end</span></em></p>
<p style="padding-left: 30px;">}</p>
<p><em><span style="color: #0000ff;">end</span></em></div>
<p>That is the basics of the code for the send method.  There are some bookkeepers that were created.  The <em>@ftp</em> object holds the ftp instance and the <em>@current_directory</em> is a multi-dimensional array that stores where the ftp server directory is and where the local directory is.  There is also a temporary object that stores the remote server&#8217;s contents called <em>remote_list.</em> Storing these locally allows us to reduce the amount of remote calls to the server because network communication <strong>ALWAYS</strong> has a varying time.  One of the nice things about sending is having the ability to check if a directory exist.  Ruby FTP can&#8217;t tell you if a remote object is a directory, or if the remote directory exist, automatically, it takes a little bit of work to find out.</p>
<p>Something else to note, it is not necessary to use the bookkeepers such as <em>@current_directory</em>.  It is possible to pass the local directory and remote directory as parameters into the recursive function instead of building a list to handle the current directories.  This type of design would be best suited for a standalone function that does not use the data found in the list but the method written above is used as a class method.  The following is a simple recursive method, assuming that the @ftp instance is already created:</p>
<p><em><span style="color: #0000ff;">def</span> <span style="color: #ff00ff;">send_it</span>(remote,local)</em></p>
<p style="padding-left: 30px;"><em>#open local and remote directories<br />
<span style="color: #339966;"> Dir</span>.chdir(local)</em><br />
<em><span style="color: #ffcc00;"> <span style="color: #ff6600;">@ftp</span></span>.chdir(remote)</em></p>
<p style="padding-left: 30px;"><em>#make a local copy of file list to reduce network calls<br />
remote_list = <span style="color: #ff6600;">@ftp</span>.nlst()</em></p>
<p style="padding-left: 30px;"><em><span style="color: #339966;">Dir</span>.entries(&#8221;.&#8221;).each { |thefile|</em></p>
<p style="padding-left: 60px;"><em>#be sure not to try and send the current and parent directory<br />
<span style="color: #0000ff;"> if</span> thefile != &#8220;.&#8221; <span style="color: #0000ff;">and</span> thefile != &#8220;..&#8221;</em></p>
<p style="padding-left: 60px;"><em>filename = <span style="color: #339966;">File</span>.basename(thefile)<br />
#check if this is a file<br />
<span style="color: #0000ff;"> if</span> <span style="color: #0000ff;">not</span> <span style="color: #339966;">File</span>.directory?(thefile)</em></p>
<p style="padding-left: 90px;"><em><span style="color: #ffcc00;"> <span style="color: #ff6600;">@ftp</span></span>.putbinaryfile(thefile,thefile)</em></p>
<p style="padding-left: 60px;"><em><span style="color: #0000ff;">else</span></em></p>
<p style="padding-left: 90px;"><em><span style="color: #0000ff;">if not</span> remote_list.index(filename)</em></p>
<p style="padding-left: 120px;"><em><span style="color: #ffcc00;"><span style="color: #ff6600;">@ftp</span>.</span>mkdir(filename)</em></p>
<p style="padding-left: 90px;"><em><span style="color: #0000ff;"> end</span></em></p>
<p style="padding-left: 60px;"><em>#make recursive call<br />
send_it(filename,filename)</em></p>
<p style="padding-left: 60px;"><em>#move back up to parent directory after sending directory</em></p>
<p style="padding-left: 60px;"><em><span style="color: #ffcc00;"><span style="color: #ff6600;">@ftp</span></span>.chdir(&#8221;..&#8221;)<br />
<span style="color: #339966;"> Dir</span>.chdir(&#8221;..&#8221;)<br />
<span style="color: #0000ff;"> end</span></em></p>
<p style="padding-left: 60px;"><em><span style="color: #0000ff;"> end</span></em></p>
<p style="padding-left: 30px;">}</p>
<p><em><span style="color: #0000ff;">end</span></em></p>
<p>FTP also fails miserably in deleting remote directories.  To delete a remote directory, all of its contents must be deleted and that includes its subfolders and contents.  Downloading a directory suffers from the same issues as sending and deleting.  Using recursion, it is easy to do both methods.  The code for each method, deleting and sending, is very similar to send_it and can be found in the link at the bottom of this page.</p>
<p>Of course, what would a program be without the use of classes.  Wrapping these methods in classes allows me to run these operations over again easily and possibly build a GUI on top of it.  The current product allows me to do stuff like this:</p>
<div id="codebox" style="overflow: auto; ">
<p><em>require <span style="color: #0000ff;">&#8216;Multi_FTP&#8217;</span><br />
testme = <span style="color: #008000;">Multi_FTP</span>.new()<br />
testme.setup(&#8221;username&#8221;,&#8221;password&#8221;,&#8221;server&#8221;)<br />
testme.go_get(&#8221;/local/destination&#8221;,&#8221;/remote/goal&#8221;)<br />
testme.go_send(&#8221;/local/sendthis&#8221;,&#8221;/remote/tohere&#8221;)<br />
testme.delete_directory(&#8221;/remote/delete_test&#8221;)<br />
testme.close_ftp()</em></div>
<p>The code is pretty easy to follow.  I create a Multi-FTP class.  Then I pass in my credentials and connect to the server.  What follows is a series of send and receives and a delete command.</p>
<p>Of course it isn&#8217;t perfect.  It could use a GUI and other stuff.  Adding a method that changes permissions is pretty simple.  It would mirror the delete method, but since I didn&#8217;t need it at the time I created the class, I never wrote it.  I didn&#8217;t test all of the error handling but looks golden.  It was fun making it though <img src='http://tylershome.nfshost.com/home/public/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>I modified some of the code so that it will work on different types of FTP versions.  Some FTP servers don&#8217;t allow deleting folders, so I just delete the files in a directory and alert the user which folders could not be deleted.  I also created a function, called <em>get_files_and_directories()</em>, that addresses how to distinguish between a file and a directory on an FTP server.  This is great for checking if a directory exist on a remote server.</p>
<p>The class can be found by clicking <a title="HERE" href="http://tylershome.nfshost.com/Multi_FTP.zip" target="_blank">HERE.</a></p>
<p style="text-align: left;">
]]></content:encoded>
			<wfw:commentRss>http://www.tylerclemons.com/ftp-fun-with-ruby/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
