<?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>Easy Ruby On Rails Programming &#187; Sample Code</title>
	<atom:link href="http://easyrubyonrailsprogramming.com/tag/sample-code/feed/" rel="self" type="application/rss+xml" />
	<link>http://easyrubyonrailsprogramming.com</link>
	<description>The Place to be to Learn Ruby On Rails</description>
	<lastBuildDate>Sat, 24 Dec 2011 05:30:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>The &#8216;yield&#8217; statement</title>
		<link>http://easyrubyonrailsprogramming.com/development/the-yield-statement/</link>
		<comments>http://easyrubyonrailsprogramming.com/development/the-yield-statement/#comments</comments>
		<pubDate>Fri, 10 Dec 2010 10:17:46 +0000</pubDate>
		<dc:creator>Avatar</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Programming Basics]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Sample Code]]></category>

		<guid isPermaLink="false">http://easyrubyonrailsprogramming.com/development/the-yield-statement/</guid>
		<description><![CDATA[It might be almost similar but relatively different in a big way for blocks may appear only in the source adjacent to a method call which means it should be written on the same line as the method&#8217;s last parameter and it is not implemented once it is encountered but, Ruby rather remembers the context [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.protsyk.com/cms/%3Fp%3D147"><img src="/wp-content/uploads/scraped/23.jpg"/></a>
<p>It might be almost similar but relatively different in a big way for blocks may appear only in the source adjacent to a method call which means it should be written on the same line as the method&#8217;s last parameter and it is not implemented once it is encountered but, Ruby rather remembers the context by which the block of code appears then enters the method. Within the method itself, the block of code may be called as if it were a block in itself by using the &#8216;yield&#8217; statement. After the block of code has been executed, control returns immediately right after the call to the yield statement. Sample use of &#8216;yield&#8217;:</p>
<p>def threeTimes<br />
     yield<br />
     yield<br />
     yield<br />
end<br />
threeTimes {puts &#8220;Hi There&#8221;}</p>
<p>The above code giving the output:<br />
Hi There<br />
Hi There<br />
Hi There</p>
<p>The code between the curly braces is associated to the method threeTimes and within that &#8216;yield is called three times in succession, each time calling the code contained within the block giving the three greeting statements. We will discuss the concepts behind the &#8216;yield&#8217; statement in the next posts as we continue to build-up up our skills.</p>
]]></content:encoded>
			<wfw:commentRss>http://easyrubyonrailsprogramming.com/development/the-yield-statement/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The &#8216;if&#8217; Statement</title>
		<link>http://easyrubyonrailsprogramming.com/development/the-if-statement/</link>
		<comments>http://easyrubyonrailsprogramming.com/development/the-if-statement/#comments</comments>
		<pubDate>Wed, 10 Nov 2010 10:15:40 +0000</pubDate>
		<dc:creator>Avatar</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Programming Basics]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Sample Code]]></category>

		<guid isPermaLink="false">http://easyrubyonrailsprogramming.com/development/the-if-statement/</guid>
		<description><![CDATA[As stated in the past post, we aim to simplify further the code we have just created with the following shorter version of the same program. class BookList def [](key) if key.kind_of?(integer) result = @Books[key] else result = @Books.find { &#124;aBooks&#124; key == sBooks.name} end return result end end Further shortening the code we have [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.ondotnet.com/pub/a/dotnet/2005/04/18/liberty.html%3Fpage%3D2"><img src="/wp-content/uploads/scraped/22.jpg"/></a>
<p>As stated in the past post, we aim to simplify further the code we have just created with the following shorter version of the same program.</p>
<p>class BookList<br />
     def [](key)<br />
	if key.kind_of?(integer)<br />
	     result =  @Books[key]<br />
	else<br />
	     result = @Books.find { |aBooks| key == sBooks.name}<br />
     	end<br />
	return result<br />
     end<br />
end</p>
<p>Further shortening the code we have above by using the ‘if’ statement as a modifier it becomes a shorter version of its former self as:</p>
<p>class Booklist<br />
     def [](key)<br />
	return @Books[key] if key.kind_of?(Integer)<br />
	return @Books.find { |aBooks| aBooks.name == key }<br />
     end<br />
end</p>
<p>The use of the &#8216;find&#8217; command in Ruby is simply a call to a function that is executed and it can be compared to a block call in many other languages such as Perl, C++ or Java. </p>
]]></content:encoded>
			<wfw:commentRss>http://easyrubyonrailsprogramming.com/development/the-if-statement/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The [] method</title>
		<link>http://easyrubyonrailsprogramming.com/development/the-method/</link>
		<comments>http://easyrubyonrailsprogramming.com/development/the-method/#comments</comments>
		<pubDate>Sun, 10 Oct 2010 10:11:15 +0000</pubDate>
		<dc:creator>Avatar</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Programming Basics]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Sample Code]]></category>

		<guid isPermaLink="false">http://easyrubyonrailsprogramming.com/development/the-method/</guid>
		<description><![CDATA[We now go through a process which implements the code on the [] method we just showed where we will take a string, searches for the book with the same title. The method would go through the whole table one at a time to get a match as shown with the following: class BookList def [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.mines.edu/~jamcneil/"><img src="/wp-content/uploads/scraped/21.jpg"/></a>
<p>We now go through a process which implements the code on the [] method we just showed where we will take a string, searches for the book with the same title. The method would go through the whole table one at a time to get a match as shown with the following:</p>
<p>class BookList<br />
     def [](key)<br />
	if key.kind_of?(integer)<br />
	     return @Books[key]<br />
	else<br />
	     for C in 0&#8230;@Books.length<br />
		return @Books[i] if key == @Book[i].name<br />
	     end<br />
	end<br />
	return nil<br />
     end<br />
end</p>
<p>The above method is a bit too close for comfort for it asks the array which one is a match and does so while going through each and every member of the list. The find method shown below is faster and easier and more natural to do this by simply using the find command shown in the next post.</p>
]]></content:encoded>
			<wfw:commentRss>http://easyrubyonrailsprogramming.com/development/the-method/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simplifying the code</title>
		<link>http://easyrubyonrailsprogramming.com/development/simplifying-the-code/</link>
		<comments>http://easyrubyonrailsprogramming.com/development/simplifying-the-code/#comments</comments>
		<pubDate>Tue, 10 Aug 2010 10:07:05 +0000</pubDate>
		<dc:creator>Avatar</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Programming Basics]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Sample Code]]></category>
		<category><![CDATA[Simplifying code]]></category>

		<guid isPermaLink="false">http://easyrubyonrailsprogramming.com/uncategorized/simplifying-the-code/</guid>
		<description><![CDATA[We now check if the book titles are deleted from the first and end of the list which should give us &#8216;nil&#8217; when and if the list becomes void of contents. list.deleteFirst >> Books: ABDC&#8211;Author1 (1) list.deleteFirst >> Books: EFGH&#8211;Author2 (2) list.deleteLast >> Books: MNOP&#8211;Author1 (1) list.deleteLast >> Books: IJKL&#8211;Author3 (3) list.deleteLast >> nil We [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.statomatic.com/features3.htm"><img src="/wp-content/uploads/scraped/20.jpg"/></a>
<p>We now check if the book titles are deleted from the first and end of the list which should give us &#8216;nil&#8217; when and if the list becomes void of contents.</p>
<p>list.deleteFirst 	>> Books: ABDC&#8211;Author1 (1)<br />
list.deleteFirst	>> Books: EFGH&#8211;Author2 (2)<br />
list.deleteLast	>> Books: MNOP&#8211;Author1 (1)<br />
list.deleteLast	>> Books: IJKL&#8211;Author3 (3)<br />
list.deleteLast	>> nil</p>
<p>We now include the use of the square brackets which allows us to acces the specific elements by the index instead of having to define each item specifically.</p>
<p>class BookList<br />
	def[](key)<br />
		if key.kind_of?(Integer)<br />
			@Books[key]<br />
		else<br />
			# &#8230;<br />
		end<br />
	end<br />
end</p>
<p>Testing the code is also very straightforward:</p>
<p>list[0]		>> Books: ABCD &#8212; Author1 (1)<br />
list[2]		>> Books: IJKL &#8212; Author  (3)<br />
list[9]		>> nil	</p>
<p>The next posts will add more functions and operators to further simplify our code making it shorter and making you more familiar with the syntax of Ruby.</p>
]]></content:encoded>
			<wfw:commentRss>http://easyrubyonrailsprogramming.com/development/simplifying-the-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Append</title>
		<link>http://easyrubyonrailsprogramming.com/development/append/</link>
		<comments>http://easyrubyonrailsprogramming.com/development/append/#comments</comments>
		<pubDate>Sat, 10 Jul 2010 10:02:53 +0000</pubDate>
		<dc:creator>Avatar</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Programming Basics]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Sample Code]]></category>

		<guid isPermaLink="false">http://easyrubyonrailsprogramming.com/development/append/</guid>
		<description><![CDATA[Using the append command/method, we can add an element to the end of the table which in turn returns a reference to the current BookList object.The deleteFirst and deleteLast command allows us to delete the first and last records respectively in the following form/s: For Append: class BookList def append(aBook) @books.push(aBook) self end end For [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.main.nc.us/openstudio/tinamanley/Iraq/append.htm"><img src="/wp-content/uploads/scraped/19.jpg"/></a>
<p>Using the append command/method, we can add an element to the end of the table which in turn returns a reference to the current BookList object.The deleteFirst and deleteLast command allows us to delete the first and last records respectively in the following form/s:</p>
<p>For Append:<br />
class BookList<br />
	def append(aBook)<br />
		@books.push(aBook)<br />
		self<br />
	end<br />
end</p>
<p>For deleteFirst and deleteLast</p>
<p>class BookList<br />
	def deleteFirts<br />
		@books.shift<br />
	end<br />
	def deleteLast<br />
		@books.pop<br />
	end<br />
end</p>
<p>Now we test the code we have just created to show if the right operations are being performed and if we are getting the results we desire:</p>
<p>list = BookList.new<br />
list.<br />
	append(Books.new(&#8216;ABCD&#8217;, &#8216;Author1&#8242;,1)).<br />
	append(Books.new(&#8216;EFGH&#8217;, &#8216;Author2&#8242;,2)).<br />
	append(Books.new(&#8216;IJKL&#8217;, &#8216;Author3&#8242;,3)).<br />
	append(Books.new(&#8216;MNOP&#8217;, &#8216;Author4&#8242;,4))</p>
<p>In the next posts, we get to check and simplify the code we have just executed.</p>
]]></content:encoded>
			<wfw:commentRss>http://easyrubyonrailsprogramming.com/development/append/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Blocks &#8211; Example</title>
		<link>http://easyrubyonrailsprogramming.com/development/blocks-example/</link>
		<comments>http://easyrubyonrailsprogramming.com/development/blocks-example/#comments</comments>
		<pubDate>Wed, 05 Mar 2008 12:16:09 +0000</pubDate>
		<dc:creator>Avatar</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Programming Basics]]></category>
		<category><![CDATA[Blocks]]></category>
		<category><![CDATA[Sample Code]]></category>

		<guid isPermaLink="false">http://easyrubyonrailsprogramming.com/development/blocks-example/</guid>
		<description><![CDATA[Below, we find sample code which uses block to return the values of the Fibonacci number Series. def fibMax(max) a1, a2 =1,1 # variables that are assigned parallel values while a1]]></description>
			<content:encoded><![CDATA[<p><a href="http://bobspaintshop.com/gpage5.html"><img src="/wp-content/uploads/scraped/25.jpg"/></a>
<p>Below, we find sample code which uses block to return the values of the Fibonacci number Series.</p>
<p>def fibMax(max)<br />
     a1, a2 =1,1 # variables that are assigned parallel values<br />
     while a1 <= max<br />
	yield a1<br />
	a1, a2 = a2, a1+a2<br />
     end<br />
end<br />
fibMax(1000) {|f| print f, &#8221; &#8220;}</p>
<p>The above code gives us the value of the Fibonacci series numbers till the number 1000 which is the set maximum number to reach.</p>
<p>Sample Code Output:<br />
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987</p>
<p>The above example shows how the block(chunk of code) processed the Fibonacci Series of numbers, returning only those that fell within the set limit of 1000.</p>
]]></content:encoded>
			<wfw:commentRss>http://easyrubyonrailsprogramming.com/development/blocks-example/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Container Creation</title>
		<link>http://easyrubyonrailsprogramming.com/development/18/</link>
		<comments>http://easyrubyonrailsprogramming.com/development/18/#comments</comments>
		<pubDate>Tue, 05 Feb 2008 10:00:59 +0000</pubDate>
		<dc:creator>Avatar</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Programming Basics]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Sample Code]]></category>

		<guid isPermaLink="false">http://easyrubyonrailsprogramming.com/development/18/</guid>
		<description><![CDATA[To initialize a container we will do the same steps showing code to support the things that have to be done. We create a table where we can add elements to the contents delete objects from either the beginning or the last entries and display an entry using the index or content on the index. [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://livedocs.adobe.com/flex/3/html/layoutperformance_03.html"><img src="/wp-content/uploads/scraped/18.jpg"/></a>
<p>To initialize a container we will do the same steps showing code to support the things that have to be done. We create a table where we can add elements to the contents delete objects from either the beginning or the last entries and display an entry using the index or content on the index. We initialize/create the array with the following syntax:</p>
<p>class BookList<br />
     def initialize<br />
	@books = Array.new<br />
     end<br />
end</p>
<p>We now have a table of book titles to which to add, delete and move data from. In the next post we will use the ‘append’ command to input data into the container we have just created.</p>
]]></content:encoded>
			<wfw:commentRss>http://easyrubyonrailsprogramming.com/development/18/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

