<?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>RubyLearning Blog &#187; Fabio Akita</title>
	<atom:link href="http://rubylearning.com/blog/author/fabioakita/feed/" rel="self" type="application/rss+xml" />
	<link>http://rubylearning.com/blog</link>
	<description>Helping Ruby Programmers become Awesome</description>
	<lastBuildDate>Tue, 24 Apr 2012 22:15:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Does ROR deployment deprive YOU of your sleep?</title>
		<link>http://rubylearning.com/blog/2010/10/08/does-ror-deployment-deprive-you-of-your-sleep/</link>
		<comments>http://rubylearning.com/blog/2010/10/08/does-ror-deployment-deprive-you-of-your-sleep/#comments</comments>
		<pubDate>Thu, 07 Oct 2010 23:30:12 +0000</pubDate>
		<dc:creator>Fabio Akita</dc:creator>
				<category><![CDATA[Beginners]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby Masters]]></category>
		<category><![CDATA[fabio akita]]></category>
		<category><![CDATA[Inploy]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Ruby Beginners]]></category>
		<category><![CDATA[Ruby for Noobs]]></category>

		<guid isPermaLink="false">http://rubylearning.com/blog/?p=4980</guid>
		<description><![CDATA[Inploy: The No Brainer Deployment Solution This guest post is contributed by Fabio Akita, who works as Project Manager for GoNow Tecnologia, in Brazil, leading Ruby on Rails projects. He worked for Locaweb, the largest web hosting company in Latin America where he helped implement the support for Rails in a shared web hosting for [...]<p><a href="http://www.launchbit.com/az/113-209/"><img width="468" height="60" src="http://www.launchbit.com/az-images/113-209/" /></a><br />
<small>(Powered by <a href="http://www.launchbit.com/lb/113-209/">LaunchBit</a>)</small></p>
]]></description>
			<content:encoded><![CDATA[<p></p>
<div class="topsy_widget_data topsy_theme_brick-red" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Frubylearning.com%252Fblog%252F2010%252F10%252F08%252Fdoes-ror-deployment-deprive-you-of-your-sleep%252F%22%2C%20%22shorturl%22%3A%20%22http%3A%2F%2Fbit.ly%2Fd8v2FW%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Does%20ROR%20deployment%20deprive%20YOU%20of%20your%20sleep%3F%22%20%7D);"></div>
<div>
<h3>Inploy: The No Brainer Deployment Solution</h3>
<p class="update">This guest post is contributed by <strong>Fabio Akita</strong>, who works as Project Manager for GoNow Tecnologia, in Brazil, leading Ruby on Rails projects. He worked for Locaweb, the largest web hosting company in Latin America where he helped implement the support for Rails in a shared web hosting for the first time. He and Locaweb also joined forces to create the successful Rails Summit Conference which now changed to RubyConf Brasil. He worked as Brazil Rails Practice Manager for Surgeworks LLC. Fabio travelled all around Brazil evangelizing the Ruby on Rails Ecosystem and modern Agile-based best practices for project management. He blogs at <a href="http://akitaonrails.com/">AkitaOnRails.com</a>.</p>
<p><span class="drop_cap">R</span>uby on Rails deployment is something that has been constantly evolving over the years. For many people &#8220;deployment&#8221; still means connecting through FTP to a remote server and dragging and dropping a bunch of files to a folder.</p>
<p>You can certainly do something similar with Rails. Products such as <a href="http://www.modrails.com/">Phusion Passenger</a> for Apache/Nginx makes this process easier: you just define a folder and drop files in there.</p>
<p class="block"><img style=' float: right; padding: 4px; margin: 0 0 2px 7px;'  class="size-full wp-image-4989 alignright" src="http://rubylearning.com/blog/wp-content/uploads/Fabio-Akita.jpeg" alt="Fabio Akita" title="Fabio Akita" width="200" height="256" /></p>
<p>Problem is, this is usually not something you do just once. You will fix bugs, improve your code, add new features, and you will keep on redeploying, overwriting old files and so on. The process of manually moving files around for deployment, even for small websites, can lead to unexpected human failure and, so, sleep deprivation, which is bad. So this process is not recommended and considered a big liability. Rule of thumb: <em>thou shall not update production servers manually</em>.</p>
<p>We all had bad experiences like this before. We also know that the best route for a safe deployment is proper automation. System administrators should always strive to automate as much as possible. If you are repeating trivial tasks manually, you are probably doing it wrong.</p>
<p>We have the hardcore tools such as Puppet and Chef, but for small to medium deployments we always relied on the good old Capistrano. For a long time it was the de facto way to automate deployments, even to many machines. It was able to do the initial setup, push new versions of your apps to your production machines reliably and even to maintain old versions so you could easily rollback to a working version with a single command.</p>
<p>Capistrano, created and maintained by Rails Core Team Alumni Jamis Buck, was a great tool. It helped perfect the Net::SSH libraries so you can script SSH connections through Ruby.</p>
<p>But we learned new tricks. One big change since then is that we started adopting Git as the version control system of choice. We can leverage the same thing that Capistrano provided in a much simpler way leveraging tools such as Git to simplify the whole process.</p>
<p>Thus, <strong><a href="http://github.com/dcrec1/inploy">Inploy</a></strong> was born, by the skillful hands of <a href="http://www.mouseoverstudio.com/blog/">Diego Carrion</a>. His intentions were clear: Capistrano is way big and complex for simpler deployments. You need to customize too much for stuff that could have been the default way of doing things. <em>Convention over Configuration</em>, that&#8217;s what Inploy is all about.</p>
<p>Right now it supports Mongrel, Thin, Passenger and Unicorn. It assumes you are using Git. It already defines many tasks that can be executed either remotely or locally, such as restarting the web application server&#8217;s processes or running Rails&#8217; database migrations. It also supports templates, so that you can reuse a specific deployment strategy across many hosting solutions. It doesn&#8217;t have nearly as many options as Capistrano, so it is not a full replacement. This is an alternative for small to medium deployments.</p>
<p>To be a competent Ruby on Rails developer, we always assume that you already know the basics of system administration at the very least, such as installing and hardening your own Linux box, installing and configuring a web server, installing a database system, setting up your firewall and so on, so I won&#8217;t detail into those simple tasks.</p>
<p>Because Inploy aims for simplicity, even its source code is very minimal and you can skim through it very easily. So I recommend you take a look at it to understand its features and how it is organized. Just to give you an idea, the entire project, if you count raw lines and add the specs and tests, has less than 1.5k lines of code.</p>
<p>You can install Inploy as a gem:</p>
<pre>gem install inploy</pre>
<p>Then, in your Rails project, you need to create a <tt>config/deploy.rb</tt> file. You should have something like this:</p>
<pre>application = "signal"
repository = 'git://github.com/dcrec1/signal.git'
hosts = ['hooters', 'geni']

#path = '/var/local/apps'
#user = 'deploy'
#ssh_opts = ''
#branch = 'master'
#sudo = false
#cache_dirs = %w(public/cache)
#skip_steps = nil
#app_folder = nil</pre>
<p>The best way to understand these options is to actually read the <tt>lib/deploy.rb</tt> from the Inploy project, which has only 100 lines of code. Then you will start figuring out where and how those variables are used. For example:</p>
<pre>def remote_setup
  remote_run "cd #{path} &amp;&amp; #{@sudo}git clone --depth 1 #{repository} #{application} &amp;&amp; cd #{application_folder} #{checkout}#{bundle} &amp;&amp; #{@sudo}rake inploy:local:setup environment=#{environment}#{skip_steps_cmd}"
end</pre>
<p>The <tt>remote_run</tt> method will connect to all the hosts defined in the <tt>hosts</tt> array and execute the following command. Then the variables are used to create the <tt>git clone</tt> task. Notice that it also runs the <tt>inploy:local:setup</tt> Rake task. It is there if you need &#8211; for some reason &#8211; to manually log in the server and run <tt>rake inploy:local:setup</tt>.</p>
<p>The DSL is very simple and is entirely based on Module Mixins. Therefore you can also create <strong>Templates</strong>, which are simple Ruby Modules that override the default methods. That way you can customize Inploy for any specific Web Hosting provider and mix in to your <tt>config/deploy.rb</tt> file.</p>
<p>Once you configure the <tt>config/deploy.rb</tt> file, you will have to run this command just once:</p>
<pre>inploy setup</pre>
<p>This will connect to all the hosts specified in the configuration file, and make the initial setups.</p>
<p>Then, every time you need to redeploy, you just run this command:</p>
<pre>inploy</pre>
<p>Under the hood, the setup task will create a git clone from the git repository you specified (make sure both your development machine and your production server have access to this git repository). Then, when you redeploy it is just a matter of running <tt>git pull</tt> to get the newest code and run maintenance tasks such as <tt>db:migrate</tt></p>
<p>There are a lot of tasks Inploy already automate out of the box such as running asset packager if you are using it, informing HopToad if you&#8217;re using it and so on. You can override the <tt>before_restarting_server</tt> if you want to add more update tasks or you can override the entire <tt>after_update_code</tt> method to add your own sequence of update tasks.</p>
<p>The most current version also includes a <strong>Rails 3 Push</strong> template strategy. You can simply add this line at the top of your <tt>config/deploy.rb</tt> file:</p>
<pre>template = "rails3_push"</pre>
<p>Now the <tt>inploy setup</tt> command will configure an inplace Git repository approach. It will create a Git repository directly on your production machine. Then every time you run the <tt>inploy</tt> command it will <tt>git push</tt> your new commits to this repository. Then, there is no step 3! It doesn&#8217;t need to <tt>git pull</tt> because you already pushed to the location where your web server is already pointing at. This is a very simple, Heroku-like, kind of deployment.</p>
<p>And if you have an empty box, with only SSH enabled, you can still automate the installation of packages and other stuff through a web available script. For example, let&#8217;s say you have a web server that contains recipes (Bash scripts) at <tt>http://myserver.com/myscript</tt>. You can execute it on your empty box like this:</p>
<pre>inploy install from=http://myserver.com/myscript</pre>
<p>And then you can do <tt>inploy setup</tt> and <tt>inploy</tt> as usual.</p>
<p>One of most notable omissions from Inploy, compared to Capistrano, is the equivalent to <tt>cap rollback</tt>. The entire directory structure and symlink strategy used by Capistrano is done in such a way to make it easier to rollback to previous versions by creating time stamped folders and symlinks to point to the current version.</p>
<p>It is a clever strategy, but maybe a bit too much for small/medium projects. If you already have Git and you already tag the versions properly, it is only a matter of <tt>git checkout old_tag</tt> to rollback to a previous version.</p>
<p>Inploy, by itself, does not implement a <tt>rollback</tt> option because it was never requested so, which means that this is a feature that people rarely use. Most of the time, if you do proper testing and continuous integration, you can safely assume that it wouldn&#8217;t break in production. This is not 100%, of course, but it means that the chance of you needing a rollback feature is drastically diminished.</p>
<p>As you can see, Inploy is a very pragmatic, thin wrapper around SSH automation, that is very easy to set up and use. Try to study the <a href="http://github.com/dcrec1/inploy">source code</a>, you will be surprised to be able to understand it very easily and quickly customize it for your particular needs.</p>
<p><em>I hope you found this article valuable. Feel free to ask questions and give feedback in the comments section of this post. Thanks!</em></p>
<p><strong><em>Do read</em> these awesome Guest Posts:</strong></p>
<ul>
<li><a href="http://rubylearning.com/blog/2010/10/07/do-you-know-rubys-chainsaw-method/">Do YOU know Ruby’s ‘Chainsaw’ method?</a></li>
<li><a href="http://rubylearning.com/blog/2010/10/01/an-introduction-to-eventmachine-and-how-to-avoid-callback-spaghetti/">An introduction to eventmachine, and how to avoid callback spaghetti</a></li>
<li><a href="http://rubylearning.com/blog/2010/09/30/the-testing-mindset/">The Testing Mindset</a></li>
<li><a href="http://rubylearning.com/blog/2010/09/29/an-introduction-to-desktop-apps-with-ruby/">An Introduction to Desktop Apps with Ruby</a></li>
<li><a href="http://rubylearning.com/blog/2010/09/28/the-ruby-movement/">The Ruby movement</a></li>
<li><a href="http://rubylearning.com/blog/2010/09/27/almost-everything-is-an-object-and-everything-is-almost-an-object/">Almost everything is an object (and everything is almost an object!)</a></li>
<li><a href="http://rubylearning.com/blog/2010/09/24/so-youre-new-to-ruby/">So… you’re new to Ruby!</a></li>
<li><a href="http://rubylearning.com/blog/2010/09/23/incorporating-web-apis-to-spark-computer-programming-exercises/">Incorporating Web APIs to spark computer programming exercises</a></li>
<li><a href="http://rubylearning.com/blog/2010/09/22/14-ways-to-have-fun-coding-ruby/">14 Ways To Have Fun Coding Ruby</a></li>
<li><a href="http://rubylearning.com/blog/2010/09/21/writing-modular-web-applications-with-rack/">Writing modular web applications with Rack</a></li>
<li><a href="http://rubylearning.com/blog/2010/09/20/how-to-learn-ruby-or-any-programming-language/">How to Learn Ruby (or any programming language)</a></li>
</ul>
</div>
<p>Technorati Tags: <a href="http://technorati.com/tag/Ruby" rel="tag">Ruby</a>, <a href="http://technorati.com/tag/Programming" rel="tag"> Programming</a>, <a href="http://technorati.com/tag/Fabio+Akita" rel="tag"> Fabio Akita</a>, <a href="http://technorati.com/tag/Ruby+for+Noobs" rel="tag"> Ruby for Noobs</a>, <a href="http://technorati.com/tag/Ruby+beginners" rel="tag"> Ruby beginners</a></p>
Posted by <b>Fabio Akita</b><p><a href="http://www.launchbit.com/az/113-209/"><img width="468" height="60" src="http://www.launchbit.com/az-images/113-209/" /></a><br />
<small>(Powered by <a href="http://www.launchbit.com/lb/113-209/">LaunchBit</a>)</small></p>

]]></content:encoded>
			<wfw:commentRss>http://rubylearning.com/blog/2010/10/08/does-ror-deployment-deprive-you-of-your-sleep/feed/</wfw:commentRss>
		<slash:comments>32</slash:comments>
		</item>
		<item>
		<title>&#8220;AkitaOnRails&#8221; On Anatomy of Ruby Blocks/Closures</title>
		<link>http://rubylearning.com/blog/2007/11/30/akitaonrails-on-anatomy-of-ruby-blocksclosures/</link>
		<comments>http://rubylearning.com/blog/2007/11/30/akitaonrails-on-anatomy-of-ruby-blocksclosures/#comments</comments>
		<pubDate>Fri, 30 Nov 2007 12:28:02 +0000</pubDate>
		<dc:creator>Fabio Akita</dc:creator>
				<category><![CDATA[Beginners]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Training]]></category>

		<guid isPermaLink="false">http://rubylearning.com/blog/2007/11/30/akitaonrails-on-anatomy-of-ruby-blocksclosures/</guid>
		<description><![CDATA[Fabio Akita&#8217;s &#8220;AkitaOnRails&#8221; series at RubyLearning.com, for would-be Ruby developers, has been quite a hit. Today in another article, Fabio talks in depth about Ruby&#8217;s Blocks/Closures, This is a rather long article but well worth the time invested in reading it. The entire source code for the programs in this article is available here. Fabio [...]<p><a href="http://www.launchbit.com/az/113-209/"><img width="468" height="60" src="http://www.launchbit.com/az-images/113-209/" /></a><br />
<small>(Powered by <a href="http://www.launchbit.com/lb/113-209/">LaunchBit</a>)</small></p>
]]></description>
			<content:encoded><![CDATA[<p></p>
<div class="topsy_widget_data topsy_theme_brick-red" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Frubylearning.com%252Fblog%252F2007%252F11%252F30%252Fakitaonrails-on-anatomy-of-ruby-blocksclosures%252F%22%2C%20%22shorturl%22%3A%20%22http%3A%2F%2Fbit.ly%2F97Jc8R%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22%5C%22AkitaOnRails%5C%22%20On%20Anatomy%20of%20Ruby%20Blocks%2FClosures%22%20%7D);"></div>
<div>
<p><em><strong>Fabio Akita&#8217;s</strong> &#8220;AkitaOnRails&#8221; series at RubyLearning.com, for would-be Ruby developers, has been quite a hit. Today in another article, Fabio talks in depth about Ruby&#8217;s <strong>Blocks/Closures</strong>, This is a rather long article but well worth the time invested in reading it. The entire source code for the programs in this article is available <a href="http://tinyurl.com/2zjgjb">here</a>.</em></p>
<p class="block"><img style=' float: left; padding: 4px; margin: 0 7px 2px 0;'  class="alignleft" title="Fabio Akita" src="http://www.rubylearning.com/images/akita.jpg" alt="Fabio Akita" /><strong>Fabio Akita</strong> is a Brazilian Rails enthusiast, also known online as &#8220;AkitaOnRails&#8221;. He regularly write posts on his own <a href="http://www.akitaonrails.com/">blog</a> and had published the very first book tailored for the Brazilian audience called &#8220;Repensando a Web com Rails&#8221;. He is now a full-time Ruby on Rails developer working as Brazil Rails Practice Manager for the Utah company Surgeworks LLC.</p>
<p><strong>Note</strong>: You may want to brush up on <a href="http://rubylearning.com/satishtalim/ruby_blocks_and_procs.html">Ruby Blocks and Procs</a> before going through Fabio&#8217;s article.</p>
<p><strong>Fabio&gt;&gt;</strong> There is no Rubyism more difficult to explain, than a Closure. I could write an entire book about it. Instead of boring everyone to death with academics, I&#8217;ll try to focus only on what one really needs for day-to-day activities.</p>
<p>Let&#8217;s start with an example:</p>
<pre><code><span style="color:blue"># program1.rb
for i in [1,2,3,4]
  puts i
end

a = 0
b = [1,2,3,4]
while a < b.length
  puts b[a]
  a += 1
end</span></code></pre>
<p>These are very simple iterators, similar to what we have in several languages. The first one with a &#8216;<strong>for</strong>&#8216; construct and the second with the familiar &#8216;<strong>while</strong>&#8216; statement. Nothing fancy here. But let&#8217;s see another way of accomplishing the same thing in Ruby:</p>
<pre><code><span style="color:blue"># program2.rb
[1,2,3,4].each { |i| puts i }

[1,2,3,4].each do |i|
  puts i
end</span></code></pre>
<p>Not too shabby, simpler and elegant, but that&#8217;s where people start gasping. The pipes notation is particularly threatening for non-starters. Both the brackets and the do..end notations define an enclosed piece of code that we name as &#8216;blocks&#8217; or &#8216;closures&#8217;. What&#8217;s between the pipes are like parameters to a method. It &#8216;feels&#8217; like this pseudo-code:</p>
<pre><code><span style="color:blue">def unnamed_method(i)
  puts i
end

[1,2,3,4].each(unnamed_method)</span></code></pre>
<p>This is not valid Ruby code, of course. That&#8217;s similar to what we would do in C# with delegates. We have something similar in JavaScript (using the &#8220;Prototype&#8221;:<a href="http://www.prototypejs.org/api/enumerable/each">http://www.prototypejs.org/api/enumerable/each</a> library):</p>
<pre><code><span style="color:blue">[1,2,3,4].each(function(i) {
  alert(i);
});</span></code></pre>
<p>In Javascript functions are first-class citizens of the language and they can be defined anonymously (without a name), and then passed as parameters to another function. We can manipulate and move functions all over the place.</p>
<p>Ruby doesn&#8217;t have methods as first-class citizens. We actually can extract a method from an object and wrap it around a &#8216;<strong>Method</strong>&#8216; object, but it holds the context of its object, so it is not independent.</p>
<pre><code><span style="color:blue"># program3.rb
class Test
  def initialize
    @hello = "Hello!"
  end
  def say
    @hello
  end
end

m = Test.new.method(:say)
puts m.call # => "Hello!"
puts m.class # => "Method"</span></code></pre>
<p>Here, we extracted the :say method from the instantiated Test instance. Notice that we can now manipulate the method as a normal object. Whenever we send the &#8216;<strong>call</strong>&#8216; message to the method object, it runs as if it was being executed from the context of the original object (Test.new.say). In the above example the second last statement would successfully print &#8220;Hello!&#8221;, as stored in the local instance variable @hello.</p>
<p>Although simple, we don&#8217;t do this very often. That&#8217;s because this method is bound to the original object&#8217;s context and we don&#8217;t usually want that: it would be nice to have an independent block of code. So, let&#8217;s create one very simple block of code referenced by a variable:</p>
<pre><code><span style="color:blue"># program4.rb
c = lambda { |i| puts i }
c.call(1) # => 1
c.call(2) # => 2</span></code></pre>
<p>The &#8216;<strong>lambda</strong>&#8216; keyword encloses the code within brackets as an object block, an instance of the <strong>Proc</strong> class. This object responds to the &#8216;<strong>call</strong>&#8216; method. In the last two statements we pass parameters to the &#8216;<strong>call</strong>&#8216; method and it goes to the &#8216;i&#8217; variable defined within pipes inside the block. So, it acts as an independent entity, detached from any particular class. Let&#8217;s test it:</p>
<pre><code><span style="color:blue"># program5.rb
c = lambda { |i| puts i }

class Test
  def say(block)
    block.call(self.class)
  end
end

c.call(self.class) # => Object
Test.new.say(c)    # => Test</span></code></pre>
<p>We&#8217;re using the same block defined above in the variable &#8216;c&#8217;. After the definition of the Test class, we call the block object passing &#8216;self.class&#8217; and it returns <strong>Object</strong> as a result. Then, we call the :say method from inside an instance of the Test class. The :say method calls the block giving the inner &#8216;self.class&#8217; as a block parameter. In this case it prints out &#8216;Test&#8217; instead of &#8216;<strong>Object</strong>&#8216;, meaning that the block binds itself to the enclosing scope. That&#8217;s one difference between a block and a detached object method.</p>
<p>In many ways, Blocks resembles anonymous functions from Javascript, anonymous delegates from C#, anonymous inner classes from Java. This is a very useful construct that was primarily created to better handle iterators. For instance:</p>
<pre><code><span style="color:blue"># program6.rb
[1,2,3,4].reverse_each { |i| puts i }
# => 4
# => 3
# => 2
# => 1</span></code></pre>
<p>Now, this is different from the Array&#8217;s &#8216;<strong>each</strong>&#8216; method we used before. &#8216;<strong>reverse_each</strong>&#8216; navigates backwards through the Array&#8217;s elements. It gets each element and passes it into the &#8216;i&#8217; variable, set as a parameter for the block defined within brackets.</p>
<p>In languages like Java, everything has to be defined through an interface. Enumerators are no different, and we have interfaces like &#8216;Iterator&#8217;. This method defines simple methods as &#8216;hasNext()&#8217; and &#8216;next()&#8217;. But what if we actually need something as a reverse iterator? Now we&#8217;re on our own. What if we need something more complicated like an iterator that only walks through even elements? In Ruby we can define such a method like this:</p>
<pre><code><span style="color:blue"># program7.rb
class Array
  def even
    i = 0
    while i < self.size
      yield(self[i]) if i % 2 == 0
      i += 1
    end
  end
end

[1,2,3,4,5,6].even { |i| puts i }
# => 1
# => 3
# => 5</span></code></pre>
<p>First of all, remember that Ruby&#8217;s classes are all open, so we can easily redefine the standard <strong>Array</strong> class and append new methods to it. Now pay attention to the &#8216;even&#8217; method. We implement it the usual way with a &#8216;while&#8217; loop. But the interesting bit is the &#8216;<strong>yield</strong>&#8216; keyword. Pretend that it works like a *wildcard placeholder* for blocks. In the example, when we pass the &#8216;self[i]&#8216; value as its parameter, we&#8217;re actually passing this value to the &#8216;i&#8217; parameter in the block.</p>
<p>We can rewrite this method in a slightly different way but with the same behavior:</p>
<pre><code><span style="color:blue"># program8.rb
class Array
  def even(&#038;code)
    i = 0
    while i < self.size
      code.call( self[i] ) if i % 2 == 0
      i += 1
    end
  end
end</span></code></pre>
<p>So, now we explicitly defined that the &#8216;even&#8217; method expects to receive a block, converting it to the &#8216;code&#8217; parameter. Then, inside it we send the &#8216;<strong>call</strong>&#8216; method to &#8216;code&#8217; and pass &#8216;self[i]&#8216; as its parameter. The result is exactly the same as using the <strong>yield</strong> keyword.</p>
<p>We can still do it differently:</p>
<pre><code><span style="color:blue"># program9.rb
class Array
  def even(block)
    i = 0
    while i < self.size
      block.call( self[i] ) if i % 2 == 0
      i += 1
    end
  end
end</span></code></pre>
<p>Now we&#8217;re doing it without the ampersand in the parameter. In the previous example, the ampersand operator &#8216;captures&#8217; a block into a <strong>Proc</strong> instance object. In the latest example, the &#8216;even&#8217; method expects to directly receive a <strong>Proc</strong> object, like this:</p>
<pre><code><span style="color:blue"># program10.rb
class Array
  def even(block)
    i = 0
    while i < self.size
      block.call( self[i] ) if i % 2 == 0
      i += 1
    end
  end
end
c = lambda { |i| puts i }
[1,2,3,4,5,6].even( c )</span></code></pre>
<p>Let&#8217;s go back to the Array&#8217;s &#8216;<strong>each</strong>&#8216; method as we displayed before:</p>
<pre><code><span style="color:blue">c = lambda { |i| puts i }
[1,2,3,4].each( &#038;c )</span></code></pre>
<p>A little bit different, because the &#8216;<strong>each</strong>&#8216; method doesn&#8217;t expect a <strong>Proc</strong> object as a parameter, but an actual Block. So we use the ampersand before the <strong>Proc</strong> instance variable and it kind of &#8216;expands&#8217; it back into a &#8216;raw&#8217; code block, so that the &#8216;even&#8217; method can &#8216;yield&#8217; it inside, instead of executing through the &#8216;<strong>call</strong>&#8216; method.</p>
<p>This usage of a <strong>Proc</strong> object is not as elegant as just passing a Block, but with this construct we are storing code within an object. We can define a method that receives as many blocks as we need, for instance:</p>
<pre><code><span style="color:blue"># program11.rb
def foo(name, block1, block2)
  block1.call
  puts name
  block2.call
end

foo "Fabio", lambda { puts "Hello" }, lambda { puts "World" }</span></code></pre>
<p>This example receives a normal parameter and 2 blocks instead of one. We can pass blocks as enclosed <strong>Proc</strong> objects in the parameters list as we would do with any other kind of object. We usually don&#8217;t need that many discrete blocks inside a single method. The most usual style is:</p>
<pre><code><span style="color:blue"># program12.rb
def foo( param1, param2 )
  # do something
  some_param = 1
  yield( some_param ) if block_given?
end

foo(1, 2) do |some_param|
  # do something
end</span></code></pre>
<p>So we define a normal method, with normal parameters. But inside it we ask &#8216;<strong>block_given?</strong>&#8216;. If positive, it &#8216;yields&#8217; its block passing some parameter to it (of course, parameters are optional, and you can pass as many parameters as you want to a block, even zero parameters).</p>
<p>We call the defined method as usual, passing a block at the end of the method call. By the way, here&#8217;s another way of defining a block: using the do .. end construct. There is no strict rule, but we reserve the brackets notation when the block is small and we can state it in a single line, and the do .. end notation when we have blocks with multiple statements inside.</p>
<p>There&#8217;s a gotcha:</p>
<pre><code><span style="color:blue">foo a, b do |some_param|
  # do something
end

foo a, b { |some_param| # do something }</span></code></pre>
<p>Both brackets and do .. end constructs define blocks, so at first glance the 2 above statements seems to do the same thing. But the gotcha is that in Ruby parenthesis are optional, and we&#8217;re not using them here.</p>
<p>In the first statement the block is assumed to be passed to the &#8216;foo&#8217; method as expected, with &#8216;a&#8217; and &#8216;b&#8217; as normal parameters. But the second statement guesses that &#8216;b&#8217; is a method and tries to pass the block to it. The recommendation is: if you have a method that needs both parameters and a block, enclose the parameters within parenthesis to avoid ambiguities.</p>
<p>We now understand that <strong>Blocks are pieces of code that can be exchanged between method calls, as parameters or returned values</strong>. But there is more to it:</p>
<pre><code><span style="color:blue"># program13.rb
c = lambda { |i| puts i }
c = Proc.new { |i| puts i }
c = proc { |i| puts i }</span></code></pre>
<p>The above 3 statements do the same thing: instantiate a block object. &#8216;proc&#8217; is an alias for &#8216;<strong>lambda</strong>&#8216; and they work slightly different than &#8216;<strong>Proc.new</strong>&#8216;. In Ruby 1.9, &#8216;proc&#8217; will probably be an alias for &#8216;<strong>Proc.new</strong>&#8216; instead.</p>
<p><strong>Keywords to keep in mind are</strong>:</p>
<ul>
<li><strong>lambda/Proc.new</strong> &#8211; encloses a bunch of code inside a <strong>Proc</strong> instance.</li>
<li>&#038; &#8211; ampersand, either captures a &#8216;raw&#8217; code block into a <strong>Proc</strong> object or expands the <strong>Proc</strong> object as a &#8216;raw&#8217; block.</li>
<li> {}/do..end &#8211; defines a code block.</li>
<li> || &#8211; pipes, defines the parameters of a block. If you don&#8217;t need any, just omit the pipes altogether.</li>
</ul>
<p>So, some people misinterpret Blocks as a simple function pointer, or something like Java&#8217;s anonymous inner class. That&#8217;s not the case: and here we finally boil down to &#8220;Closures&#8221;. Ruby Blocks are Closures. The words &#8216;blocks&#8217; and &#8216;closures&#8217; mean the same thing in Ruby.</p>
<p>Ruby Blocks can enclose not only code and it&#8217;s own inner local variables, but it can enclose the surrounding context variables. That&#8217;s why it is called a &#8216;closure&#8217;. Let&#8217;s see an example:</p>
<pre><code><span style="color:blue"># program14.rb
def greetings_factory(prefix)
  Proc.new { |name| "#{prefix}, #{name} !"}
end

birthday = greetings_factory("Happy Birthday")
xmas = greetings_factory("Merry XMas")

puts birthday.call("David") # => "Happy Birthday, David !"
puts xmas.call("Matz")      # => "Merry XMas, Matz !"</span></code></pre>
<p>The first thing is a method definition for &#8216;greetings_factory&#8217;. It gets a prefix as a parameter and returns a <strong>Proc</strong> object, whose inner parameter is a name. So far so good.</p>
<p>The second part defines 2 <strong>Proc</strong> instances, one for birthday and another for christmas. Notice that we pass 2 different prefixes into the &#8216;greetings_factory&#8217; method. The different values are &#8216;closed&#8217; within Block. So, when we later call them, we see how differently they behave: they actually stored the latest state within itself. So each block stored the &#8216;prefix&#8217; variable passed before, while still accepting the &#8216;name&#8217; parameter within the Block.</p>
<p>Keep in mind that every Ruby Block is a Closure, that&#8217;s why this construct actually works:</p>
<pre><code><span style="color:blue"># program15.rb
list = []
[1,2,3,4].each do |i|
  list << i * 2
end
puts list.inspect # => [2, 4, 6, 8]</span></code></pre>
<p>So, we defined a &#8216;list&#8217; array *before* we create the iterator block. Then, inside the block we refer to the external &#8216;list&#8217; array and populate it. In Java, this would&#8217;ve been a final variable, but in Ruby there is no such limitation.</p>
<p>You&#8217;d want to be very careful about the surroundings of your block: do not define variables that&#8217;s going to be used inside the blocks too early in the code. Try to keep dependencies nearby, like in the above example where the &#8216;list&#8217; Array is defined right before the iterator itself.</p>
<p>Iterators get a big boost because we&#8217;re not limited to a hard Interface. We can add whatever methods we need, like &#8216;each&#8217;, &#8216;reverse_each&#8217;, &#8216;collect&#8217;, &#8216;select&#8217; and so on. Each one of them can receive a block and pass one element at a time to the user-defined block.</p>
<p>Another very important usage is to enclose widely used code patterns. For example, Rails has the following construct to use database transactions:</p>
<pre><code><span style="color:blue">User.transaction do
  u = User.new(:login => 'admin')
  u.save!
end</span></code></pre>
<p>&#8216;User&#8217; would be the <strong>ActiveRecord</strong> instance. One example of the structure for the model&#8217;s &#8216;transaction&#8217; class method would resemble this structure:</p>
<pre><code><span style="color:blue">class ActiveRecord::Base
  def self.transaction
    begin
      ActiveRecord::Base.establish_connection
      yield if block_given?
    rescue => e
      RAILS_DEFAULT_LOGGER.error e
    ensure
      ActiveRecord::Base.remove_connection
    end
  end
end</span></code></pre>
<p>This means: open the database then try to &#8216;yield&#8217; the block if provided. If anything wrong happens, get the error message and log it. Finally ensure that the connection is dropped after all this.</p>
<p><strong>ActiveRecord</strong> doesn&#8217;t open and close connections this often, but you get the idea. But this is an overall big picture of one way to avoid repetition and the extraction of common code patterns. So it&#8217;s clever to encapsulate common functionality and place in a placeholder for user-defined code in-between.</p>
<p>The <strong>File.open</strong> method does the same thing: it takes responsibility of properly opening files, yielding the user block and ensuring that the file is closed without the user having to manually do this kind of house cleaning.</p>
<p>The most important &#8220;concept&#8221;:<a href="http://martinfowler.com/bliki/Closure.html">http://martinfowler.com/bliki/Closure.html</a> is that Blocks helps hiding implementation details. We don&#8217;t want to know the inner details of a list iterator, or how a transaction works. We just need to focus on the business logic itself, trapped within a Block.</p>
<p>We described here a lot of stuff, and I think it covers the basics. Enough to actually read some of the Rails source-code and get acquainted for closure&#8217;s modus operandi. Hope you enjoyed this article!</p>
<p><em>Thank you Fabio for showing us a different perspective on Ruby Blocks. In case you have any queries, questions on this article, kindly post your questions here and Fabio would be glad to answer.</em></p>
</div>
<p>Technorati Tags: <a href="http://technorati.com/tag/Brazil" rel="tag">Brazil</a>, <a href="http://technorati.com/tag/Fabio+Akita" rel="tag">Fabio Akita</a>, <a href="http://technorati.com/tag/Rails" rel="tag">Rails</a>, <a href="http://technorati.com/tag/Ruby+Blocks" rel="tag">Ruby Blocks</a>, <a href="http://technorati.com/tag/Ruby+Programming" rel="tag">Ruby Programming</a></p>
Posted by <b>Fabio Akita</b><p><a href="http://www.launchbit.com/az/113-209/"><img width="468" height="60" src="http://www.launchbit.com/az-images/113-209/" /></a><br />
<small>(Powered by <a href="http://www.launchbit.com/lb/113-209/">LaunchBit</a>)</small></p>

]]></content:encoded>
			<wfw:commentRss>http://rubylearning.com/blog/2007/11/30/akitaonrails-on-anatomy-of-ruby-blocksclosures/feed/</wfw:commentRss>
		<slash:comments>29</slash:comments>
		</item>
	</channel>
</rss>

