<?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"
	>

<channel>
	<title>r-williams</title>
	<atom:link href="http://www.r-williams.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.r-williams.com</link>
	<description>Random Tidbits</description>
	<pubDate>Sun, 27 Apr 2008 18:53:36 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6-bleeding</generator>
	<language>en</language>
			<item>
		<title>Simple Blocks in Ruby Classes</title>
		<link>http://www.r-williams.com/2008/04/simple-blocks-in-ruby-classes/</link>
		<comments>http://www.r-williams.com/2008/04/simple-blocks-in-ruby-classes/#comments</comments>
		<pubDate>Sat, 12 Apr 2008 23:35:44 +0000</pubDate>
		<dc:creator>rob</dc:creator>
		
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.r-williams.com/?p=3</guid>
		<description><![CDATA[Simplify your code and stay DRY by wrapping commonly used commands in a block.
If you ever find your self writing ugly code like this:

db = Database.new
db.open
orders = db.fetch_orders
db.close

It may be worth looking at ruby&#8217;s block syntax so you can instead write:

Database.new do &#124;db&#124;
  orders = db.fetch_orders
end

Just check if a block is given inside the [...]]]></description>
			<content:encoded><![CDATA[<p>Simplify your code and stay <acronym title="Don't Repeat Yourself">DRY</acronym> by wrapping commonly used commands in a block.</p>
<p>If you ever find your self writing ugly code like this:</p>
<pre name="code" class="ruby:nocontrols:nogutter">
db = Database.new
db.open
orders = db.fetch_orders
db.close
</pre>
<p>It may be worth looking at ruby&#8217;s block syntax so you can instead write:</p>
<pre name="code" class="ruby:nocontrols:nogutter">
Database.new do |db|
  orders = db.fetch_orders
end
</pre>
<p>Just check if a block is given inside the initialize method and wrap it with the open and close method calls.</p>
<pre name="code" class="ruby:nogutter">
class Database

  def initialize
    if block_given?
      open
        yield self
      close
    end
  end

  def open
  end

  def close
  end

  def fetch_orders
  end
end
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.r-williams.com/2008/04/simple-blocks-in-ruby-classes/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
