<?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>Coding Friends &#187; Ruby</title>
	<atom:link href="http://www.codingfriends.com/index.php/category/programming/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codingfriends.com</link>
	<description>Coding Friends, place for developers.</description>
	<lastBuildDate>Sun, 04 Dec 2011 21:11:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Inheritance</title>
		<link>http://www.codingfriends.com/index.php/2009/07/28/inheritance/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=inheritance</link>
		<comments>http://www.codingfriends.com/index.php/2009/07/28/inheritance/#comments</comments>
		<pubDate>Tue, 28 Jul 2009 13:17:36 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[gnu]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=274</guid>
		<description><![CDATA[Inherited classes allow for similar code to be re-used, this saves on code space and also development/testing time. Inherited classes basically have access to the internals of the &#8220;super&#8221; inherited class. To create a inherited class class sibling < superclass And the sibling class can then call any methods etc within the superclass. The code [...]]]></description>
			<content:encoded><![CDATA[<p>Inherited classes allow for similar code to be re-used, this saves on code space and also development/testing time.  Inherited classes basically have access to the internals of the &#8220;super&#8221; inherited class.</p>
<p>To create a inherited class<br />
<b>class sibling < superclass</b></p>
<p>And the sibling class can then call any methods etc within the superclass.  The code example explains in code <img src='http://www.codingfriends.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># base &quot;Books&quot; class</span>
<span style="color:#9966CC; font-weight:bold;">class</span> Books
   <span style="color:#008000; font-style:italic;"># this is called within the .new class call</span>
   <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>name, author, year<span style="color:#006600; font-weight:bold;">&#41;</span>
       <span style="color:#0066ff; font-weight:bold;">@name</span> = name;
       <span style="color:#0066ff; font-weight:bold;">@author</span> = author;
       <span style="color:#0066ff; font-weight:bold;">@year</span> = year;
   <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
   <span style="color:#9966CC; font-weight:bold;">def</span> to_s
          <span style="color:#008000; font-style:italic;"># return the to_s (to_string)</span>
          <span style="color:#996600;">&quot;Books - Name #{@name} - Author #{@author} - Year #{@year}&quot;</span>
   <span style="color:#9966CC; font-weight:bold;">end</span>          
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># this class is Inherited from the &quot;Books&quot; class</span>
<span style="color:#9966CC; font-weight:bold;">class</span> BookToCD <span style="color:#006600; font-weight:bold;">&lt;</span> Books
    <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>name, author, year, cdID<span style="color:#006600; font-weight:bold;">&#41;</span>
           <span style="color:#008000; font-style:italic;"># use the &quot;Books&quot; class initlialize to setup the local variables within the super/inherited class</span>
           <span style="color:#9966CC; font-weight:bold;">super</span><span style="color:#006600; font-weight:bold;">&#40;</span>name, author, year<span style="color:#006600; font-weight:bold;">&#41;</span>
           <span style="color:#008000; font-style:italic;">#setup the local variables</span>
           <span style="color:#0066ff; font-weight:bold;">@cdID</span> = cdID
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> to_s
           <span style="color:#008000; font-style:italic;"># get the result from the super class</span>
           result = <span style="color:#9966CC; font-weight:bold;">super</span>.<span style="color:#9900CC;">to_s</span>
           <span style="color:#008000; font-style:italic;"># return the result from the super class + the local cdID variable</span>
           <span style="color:#0000FF; font-weight:bold;">return</span> result <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">&quot; - CDID #{@cdID}&quot;</span> 
    <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#008000; font-style:italic;"># create a book</span>
book = Books.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Programming in Ruby&quot;</span>, <span style="color:#996600;">&quot;Ruby Team&quot;</span>, <span style="color:#006666;">2006</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#008000; font-style:italic;"># inspect outputs the internel structure of the class</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> book.<span style="color:#9900CC;">inspect</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> book.<span style="color:#9900CC;">to_s</span>
&nbsp;
booktocd = BookToCD.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;CD read of programming in Ruby&quot;</span>, <span style="color:#996600;">&quot;Ruby Team&quot;</span>,<span style="color:#006666;">2006</span>, <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#008000; font-style:italic;"># the inspect shows that the new class has a different memory address</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> booktocd.<span style="color:#9900CC;">inspect</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> booktocd.<span style="color:#9900CC;">to_s</span></pre></div></div>

<p>Once executed on the command line the result is</p>
<pre class="consoleoutput">
#&lt;Books:0x2777070 @year=2006, @name="Programming in Ruby", @author="Ruby Team"&gt;
Books - Name Programming in Ruby - Author Ruby Team - Year 2006
#&lt;BookToCD:0x2776f38 @year=2006, @name="CD read of programming in Ruby", @cdID=1, @author="Ruby Team"&gt;
Books - Name CD read of programming in Ruby - Author Ruby Team - Year 2006 - CDID 1
</pre>
<p>As you can see the inspect method is showing that the memory block of the book and also booktocd are not the same even though they are sharing the same internals, this is Object Orientated.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2009/07/28/inheritance/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Methods, Arrays, Hash</title>
		<link>http://www.codingfriends.com/index.php/2009/07/28/methods-arrays-hash/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=methods-arrays-hash</link>
		<comments>http://www.codingfriends.com/index.php/2009/07/28/methods-arrays-hash/#comments</comments>
		<pubDate>Tue, 28 Jul 2009 13:15:42 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[gnu]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=272</guid>
		<description><![CDATA[First part is the method and the second is the array and hash variables. The manor in which Ruby returns values from a method is either by the return keyword or by the last executed line of code, thus if the result for the return of the method is the last line then there is [...]]]></description>
			<content:encoded><![CDATA[<p>First part is the method and the second is the array and hash variables.</p>
<p>The manor in which Ruby returns values from a method is either by the return keyword or by the last executed line of code, thus if the result for the return of the method is the last line then there is no need to use the return keyword, but of course if the returnable value is calculated within the middle of the method and the rest is to clean up the method then use the return keyword.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># this is a basic method</span>
<span style="color:#9966CC; font-weight:bold;">def</span> saySomething<span style="color:#006600; font-weight:bold;">&#40;</span>name<span style="color:#006600; font-weight:bold;">&#41;</span>
       result = <span style="color:#996600;">&quot;hi there #{name}&quot;</span>
       <span style="color:#0000FF; font-weight:bold;">return</span> result
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># but Ruby will return the last evaluated line, thus this method</span>
<span style="color:#008000; font-style:italic;"># is the same as the above one.</span>
<span style="color:#9966CC; font-weight:bold;">def</span> saySomething2<span style="color:#006600; font-weight:bold;">&#40;</span>name<span style="color:#006600; font-weight:bold;">&#41;</span>
       <span style="color:#996600;">&quot;hi there #{name}&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># the two methods that are the same in there results</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> saySomething<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Ian&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> saySomething2<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Ian Porter&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Below is how Ruby uses the array structure and also the hash structure, an array is defined within the [ ] brackets and the start of the array is 0, but within a hash since there is no start to the hash keys, then there is an hash key and then the values associated with that key.  The hash structure is defined within { } brackets.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># this is a array</span>
a = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'Gentoo Linux'</span>,<span style="color:#996600;">'is'</span>,<span style="color:#996600;">'great'</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
<span style="color:#008000; font-style:italic;">#this is a hash, an hash is a key -&gt; value</span>
has = <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#996600;">'Gentoo'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'Gentoo Linux'</span>,
       <span style="color:#996600;">'RH'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'Red Hat Linux'</span><span style="color:#006600; font-weight:bold;">&#125;</span>       <span style="color:#008000; font-style:italic;"># hash</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> a<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>       <span style="color:#008000; font-style:italic;"># print the first element of the array</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> a              <span style="color:#008000; font-style:italic;"># print the arary</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;The Key value of RH within the hash&quot;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> has<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'RH'</span><span style="color:#006600; font-weight:bold;">&#93;</span>       <span style="color:#008000; font-style:italic;"># print the value assoicated with the key RH</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;The hash in full&quot;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> has</pre></div></div>

<p>Once executed from the console, the output is </p>
<pre class="consoleoutput">
hi there Ian
hi there Ian Porter
</pre>
<p>for the first method demonstrating that the result value is the same</p>
<pre class="consoleoutput">
Gentoo Linux
Gentoo Linux
is
great
The Key value of RH within the hash
Red Hat Linux
The hash in full
RHRed Hat LinuxGentooGentoo Linux
</pre>
<p>Which shows the results of the program, this shows that the two methods return in the same manor and also an array is broken into separate blocks, but a hash has a continuous memory block in nature.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2009/07/28/methods-arrays-hash/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Class And Modules</title>
		<link>http://www.codingfriends.com/index.php/2009/07/28/class-and-modules/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=class-and-modules</link>
		<comments>http://www.codingfriends.com/index.php/2009/07/28/class-and-modules/#comments</comments>
		<pubDate>Tue, 28 Jul 2009 13:12:44 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[gnu]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=267</guid>
		<description><![CDATA[A Ruby class is very similar to the other programming languages class structures. They allow for code to be created within a single object that can be reused etc, the code can have its own separate block of variables/methods for that instance of the required task. It is able to be inherited and extended which [...]]]></description>
			<content:encoded><![CDATA[<p>A Ruby class is very similar to the other programming languages class structures. They allow for code to be created within a single object that can be reused etc, the code can have its own separate block of variables/methods for that instance of the required task. It is able to be inherited and extended which is different to a module which only allows for the created module to have methods and constant variables.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> SillyModule <span style="color:#008000; font-style:italic;"># module is a single instance object, e.g. they can have methods and constants</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> hello
    <span style="color:#996600;">&quot;Hello.&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> SillyClass	<span style="color:#008000; font-style:italic;"># a class can include a module and expand on it, an class can generate many instances (objects) of itself.</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">include</span> SillyModule <span style="color:#008000; font-style:italic;"># also use the sillymodule as well.</span>
    <span style="color:#9966CC; font-weight:bold;">def</span> bob
      <span style="color:#996600;">&quot;timing is everything.&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span>
s = SillyClass.<span style="color:#9900CC;">new</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> s.<span style="color:#9900CC;">hello</span>  <span style="color:#008000; font-style:italic;"># Hello.</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> s.<span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#008000; font-style:italic;"># SillyClass - displays the class name</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> s.<span style="color:#9900CC;">bob</span> <span style="color:#008000; font-style:italic;"># timing is everything</span></pre></div></div>

<p>Save this code as class_module.rb, it will create a module that is included within the SillyClass. To demonstrate that the module is a single instance of a object, if you try to create a new object, e.g. mod = SillyModule.new, the compile will class.rb:20: undefined method `new&#8217; for SillyModule:Module (NoMethodError) since the new method is not part of a module but only a class.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2009/07/28/class-and-modules/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Method &#8211; Add Two numbers</title>
		<link>http://www.codingfriends.com/index.php/2009/07/28/method-add-two-numbers-4/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=method-add-two-numbers-4</link>
		<comments>http://www.codingfriends.com/index.php/2009/07/28/method-add-two-numbers-4/#comments</comments>
		<pubDate>Tue, 28 Jul 2009 13:09:16 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[gnu]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=265</guid>
		<description><![CDATA[This tutorial uses the same code as Add two numbers but includes a function/method to add two numbers together and return a value. The addtwo function has two parameters that are returned back added together. I have called them a and b, so that they are different names to the variables within the main method. [...]]]></description>
			<content:encoded><![CDATA[<p>This tutorial uses the same code as <a href="http://www.codingfriends.com/index.php/2009/07/28/add-two-numbers-5/" title="Add two numbers" target="_blank">Add two numbers</a> but includes a function/method to add two numbers together and return a value.  The addtwo function has two parameters that are returned back added together.  I have called them a and b, so that they are different names to the variables within the main method.</p>
<p>The source code</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># define a function called addtwo with two parameters</span>
<span style="color:#9966CC; font-weight:bold;">def</span> addtwo<span style="color:#006600; font-weight:bold;">&#40;</span>a, b<span style="color:#006600; font-weight:bold;">&#41;</span> 
       a <span style="color:#006600; font-weight:bold;">+</span> b; <span style="color:#008000; font-style:italic;"># return a + b</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">print</span> <span style="color:#996600;">&quot;Please enter number 1 : &quot;</span>;
<span style="color:#008000; font-style:italic;"># get the input from the console, </span>
val1 = <span style="color:#CC0066; font-weight:bold;">gets</span>;
<span style="color:#CC0066; font-weight:bold;">print</span> <span style="color:#996600;">&quot;Please enter number 2 : &quot;</span>;
val2 = <span style="color:#CC0066; font-weight:bold;">gets</span>;
<span style="color:#008000; font-style:italic;"># convert the string console inputs to_i (to_integers) and add together</span>
<span style="color:#CC0066; font-weight:bold;">print</span> <span style="color:#996600;">&quot;Answer : &quot;</span> , <span style="color:#006600; font-weight:bold;">&#40;</span>val1.<span style="color:#9900CC;">to_i</span> <span style="color:#006600; font-weight:bold;">+</span> val2.<span style="color:#9900CC;">to_i</span><span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#996600;">&quot;<span style="color:#000099;">\n</span>&quot;</span>;</pre></div></div>

<p>save as addtwonumbers_function.rb, this program will function the same as the previous tutorial apart from the inner working will call the method.  The method programming allows for one method to be called multiple times, e.g. Within the example above there could be a method that was 5 lines in length and instead of writing the same 5 lines each time, you just write a method that is called.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2009/07/28/method-add-two-numbers-4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Add two numbers</title>
		<link>http://www.codingfriends.com/index.php/2009/07/28/add-two-numbers-5/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=add-two-numbers-5</link>
		<comments>http://www.codingfriends.com/index.php/2009/07/28/add-two-numbers-5/#comments</comments>
		<pubDate>Tue, 28 Jul 2009 13:06:28 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[gnu]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/index.php/2009/07/28/add-two-numbers-5/</guid>
		<description><![CDATA[This tutorial will demonstrate how to read from the input console (console line) to answer the prompts. The prompts will need to be a integer value to add the two inputs together. The gets function is able to get a string from the console input, with the string inputted this allows the string function .to_i [...]]]></description>
			<content:encoded><![CDATA[<p>This tutorial will demonstrate how to read from the input console (console line) to answer the prompts.  The prompts will need to be a integer value to add the two inputs together.</p>
<p>The gets function is able to get a string from the console input, with the string inputted this allows the string function .to_i (to_integer) conversion for the answer to add up to integer values.</p>
<p>The code</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">print</span> <span style="color:#996600;">&quot;Please enter number 1 : &quot;</span>;
<span style="color:#008000; font-style:italic;"># get the input from the console, </span>
val1 = <span style="color:#CC0066; font-weight:bold;">gets</span>;
<span style="color:#CC0066; font-weight:bold;">print</span> <span style="color:#996600;">&quot;Please enter number 2 : &quot;</span>;
val2 = <span style="color:#CC0066; font-weight:bold;">gets</span>;
<span style="color:#008000; font-style:italic;"># convert the string console inputs to_i (to_integers) and add together</span>
<span style="color:#CC0066; font-weight:bold;">print</span> <span style="color:#996600;">&quot;Answer : &quot;</span> , <span style="color:#006600; font-weight:bold;">&#40;</span>val1.<span style="color:#9900CC;">to_i</span> <span style="color:#006600; font-weight:bold;">+</span> val2.<span style="color:#9900CC;">to_i</span><span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#996600;">&quot;<span style="color:#000099;">\n</span>&quot;</span>;</pre></div></div>

<p>save that as addtwonumbers.rb.</p>
<p>To run ruby addtwonumbers.rb, and the output would be similar to </p>
<pre class="consoleoutput">
Please enter value 1 : 30
Please enter value 2: 23
Answer = 53
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2009/07/28/add-two-numbers-5/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Read/Write files</title>
		<link>http://www.codingfriends.com/index.php/2009/07/28/readwrite-files-3/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=readwrite-files-3</link>
		<comments>http://www.codingfriends.com/index.php/2009/07/28/readwrite-files-3/#comments</comments>
		<pubDate>Tue, 28 Jul 2009 13:03:09 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[gnu]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=262</guid>
		<description><![CDATA[This is a tutorial on how to open and write files with Ruby, of course there is always different ways to accomplish the same task within programming languages. The objects e.g. File, have different functions associated with them. The File.new creates a new file, the IO (InputOutput) object is an allows for different Input output [...]]]></description>
			<content:encoded><![CDATA[<p>This is a tutorial on how to open and write files with Ruby, of course there is always different ways to accomplish the same task within programming languages.  </p>
<p>The objects e.g. File, have different functions associated with them.  The File.new creates a new file, the IO (InputOutput) object is an allows for different Input output tasks, which in this case to read each line of the input file (countrys.txt) and assign the value to &#8216;line&#8217;.  The puts prints out what has been read in, and the syswrite, which was created from the File.net outputs to the output file.</p>
<p>NOTE: the { } are the begin and end of a code structure, e.g. While begin; do something; end;</p>
<p>This is the code</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">OutFile = <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;sqlrubycountry.txt&quot;</span>,<span style="color:#996600;">&quot;w&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;
&nbsp;
<span style="color:#CC00FF; font-weight:bold;">IO</span>.<span style="color:#9900CC;">foreach</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;countrys.txt&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>line<span style="color:#006600; font-weight:bold;">|</span>
        <span style="color:#CC0066; font-weight:bold;">puts</span> line
        OutFile.<span style="color:#9900CC;">syswrite</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;insert into country(place) values (<span style="color:#000099;">\&quot;</span>#{line.strip}<span style="color:#000099;">\&quot;</span>);<span style="color:#000099;">\n</span>&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;
<span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
OutFile.<span style="color:#9900CC;">close</span>;</pre></div></div>

<p>if you save that as rubyreadfile.rb and also create a countrys.txt file with what ever text you like, e.g.<br />
United Kingdom<br />
France<br />
Germany<br />
United States etc.</p>
<p>The output of the program (ruby rubyreadfile.rb) will display each line that is read from the input file and the output file will have some other text in it, for demonstrating purposes, have done some sql code.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2009/07/28/readwrite-files-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hello world Ruby style</title>
		<link>http://www.codingfriends.com/index.php/2009/07/28/hello-world-ruby-style/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=hello-world-ruby-style</link>
		<comments>http://www.codingfriends.com/index.php/2009/07/28/hello-world-ruby-style/#comments</comments>
		<pubDate>Tue, 28 Jul 2009 12:57:29 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[gnu]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=260</guid>
		<description><![CDATA[To get Ruby]]></description>
			<content:encoded><![CDATA[<p>To get Ruby <a href="http://www.rubyonrails.org/down" title="http://www.rubyonrails.org/down" target="_blank"Ruby On Rails</a>.  Ruby is a development environment that is similar to other Object Oriented (OO) languages, but it is designed to be developed in quickly with a stable environment.  Ruby can be used to create GUI&#8217;s to websites very quickly.</p>
<p>This first source code is the very basic Hello word!.</p>
<p>If you save this code</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Hello World&quot;</span>;</pre></div></div>

<p>as rubyhelloworld.rb</p>
<p>The file extension rb just stands for Ruby.  To run the code from the command line, where you have saved the file above</p>
<pre class="consoleoutput">
ruby rubyhelloworld.rb
</pre>
<p>and the output will be</p>
<pre class="consoleoutput">
Hello World
</pre>
<p>Hope that helps people.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2009/07/28/hello-world-ruby-style/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

