<?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>RailsRocket</title>
	<atom:link href="http://www.railsrocket.com/feed" rel="self" type="application/rss+xml" />
	<link>http://www.railsrocket.com</link>
	<description>A Ruby on Rails community website with tutorials</description>
	<lastBuildDate>Fri, 24 Sep 2010 16:53:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>PlainRL 1.0 Released</title>
		<link>http://www.railsrocket.com/plainrl-10-released</link>
		<comments>http://www.railsrocket.com/plainrl-10-released#comments</comments>
		<pubDate>Mon, 01 Jun 2009 14:34:12 +0000</pubDate>
		<dc:creator>Ashiq Alibhai</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[PlainRL]]></category>
		<category><![CDATA[releases]]></category>

		<guid isPermaLink="false">http://www.railsrocket.com/articles/?p=260</guid>
		<description><![CDATA[PlainRL 1.0 is out! Download it now and give it a try--a full, working, albeit plain RogueLike game written entirely in Ruby. <a href="http://www.railsrocket.com/plainrl-10-released">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><!--noadsense--><br />
I&#8217;ve been working quietly for the last eight months on PlainRL, a Ruby roguelike. It&#8217;s a full, working roguelike; it has the following features:</p>
<ul>
<li><strong>Fully Random:</strong> Almost everything in the game is entirely randomly-generated from a few lines of plain-text (seed data).</li>
<li><strong>30 Floors:</strong> Thirty floors of monster-slaying mayhem, complete with several side-quests. It&#8217;s a polished, full game, replete with weapons, armour, bows, everything!</li>
<li><strong>Advanced Features:</strong> Weapon skills, player skills (both level up over time), and lots of interesting things.</li>
<li><strong>Tweakable Content:</strong> You can change a great deal by editing a few plain-text files and a single globals file.</li>
<p>If that sounds like your fancy, give it a whirl (download link at the bottom of the post). I&#8217;m thankful for all the lessons I&#8217;ve learned along the way. I feel I have a good, solid grounding in Ruby now (useful for any Rails developer).</p>
<p>Download it and take a look; it&#8217;s an interesting piece of code.</p>
<p>Download the source here: <a href='http://www.railsrocket.com/wp-content/uploads/PlainRL.zip'>PlainRL 1.0</a></p>
<p>SVN repository: <a href="http://OpenSVN.csie.org/plainrl">http://OpenSVN.csie.org/plainrl</a></p>
<p><strong>Edit:</strong> I&#8217;ve tried various versions of Curses/NCurses on Windows, and in all cases, the game just freezes if you hold down a key. If you&#8217;re on Windows, use Cygwin.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.railsrocket.com/plainrl-10-released/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Internationalizing Your Rails Application in 34 Languages eBook</title>
		<link>http://www.railsrocket.com/internationalizing-your-rails-application-in-34-languages</link>
		<comments>http://www.railsrocket.com/internationalizing-your-rails-application-in-34-languages#comments</comments>
		<pubDate>Tue, 27 Jan 2009 02:36:49 +0000</pubDate>
		<dc:creator>Ashiq Alibhai</dc:creator>
				<category><![CDATA[Deployment]]></category>
		<category><![CDATA[ebooks]]></category>
		<category><![CDATA[I18n]]></category>
		<category><![CDATA[Rails 2.2]]></category>

		<guid isPermaLink="false">http://www.railsrocket.com/articles/?p=249</guid>
		<description><![CDATA[We're very pleased to announce our very on Rails eBook: Internationalizing Your Rails Application in 34 Languages! The goal of this eBook is to help you get your Rails application internationalized--and to kick it off with not one or two, but 34 different languages--all without the expense of hiring a translator! <a href="http://www.railsrocket.com/internationalizing-your-rails-application-in-34-languages">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>We&#8217;re very pleased to announce our very on Ruby on Rails eBook: <a href='http://www.railsrocket.com/articles/wp-content/uploads/i18n-your-rails-app-in-34-languages.pdf'>Internationalizing Your Rails Application in 34 Languages!</a> The goal of this eBook is to help you get your Ruby on Rails application internationalized&#8211;and to kick it off with not one or two, but <strong>34 different languages</strong>&#8211;all without the expense of hiring a translator!</p>
<p>This eBook covers:</p>
<ul>
<li>Why bother internationalizing in the first place?</li>
<li>The Rails 2.2 API for internationalization</li>
<li>How to internationalize your Rails application</li>
<li>How to get your application translated with minimal pain</li>
<li>Using Google Translate to get quick, cheap translations of your Ruby on Rails application (and why this is important, even with a mediocre-quality translation).</li>
<li>And more!</li>
</ul>
<p>So don&#8217;t wait&#8211;<a href='http://www.railsrocket.com/articles/wp-content/uploads/i18n-your-rails-app-in-34-languages.pdf'>download it now</a> and get your Ruby on Rails application internationalized today! (Really, it takes less than an hour for the entire process.)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.railsrocket.com/internationalizing-your-rails-application-in-34-languages/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>rake secret?</title>
		<link>http://www.railsrocket.com/rake-secret</link>
		<comments>http://www.railsrocket.com/rake-secret#comments</comments>
		<pubDate>Wed, 21 Jan 2009 18:49:31 +0000</pubDate>
		<dc:creator>Ashiq Alibhai</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[history]]></category>
		<category><![CDATA[rake]]></category>
		<category><![CDATA[restful authentication]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[setup]]></category>

		<guid isPermaLink="false">http://www.railsrocket.com/articles/?p=243</guid>
		<description><![CDATA[Bort and Restful Authentication, require you to edit the REST_AUTH_SITE_KEY. What does this mean? Rake has a 'secret' target which you can call; it generates a key. What about the 'secret' value in environment.rb? Added in Rails 2.0, as part of moving session state to the client side, this key encrypts session data. <a href="http://www.railsrocket.com/rake-secret">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;ve tried using Bort, the bootstrapped Rails skeleton application on steroids, you may have noticed one instruction in the readme file that says &#8220;Edit the REST_AUTH_SITE_KEY in each of the environment files.&#8221;</p>
<p>Huh? REST_AUTH_SITE_KEY? Some googling revealed fairly little information. After some digging, though, the pieces fell into place; so what follows is a (hopefully thorough-enough) explanation.</p>
<p>First of all, to generate a secret key, just use the &#8220;secret&#8221; rake command:</p>
<p><code>rake secret</code></p>
<p>This will generate a sufficiently-long, sufficiently-strong key. And you&#8217;re done. Restful Authentication is happy.</p>
<p>Interestingly enough, if you look in <code>config/environment.rb</code>, under <code>config.action_controller.session</code>, there&#8217;s something called &#8220;secret.&#8221; And this is the very thing that spurned the creation of the <code>secret</code> rake task.</p>
<p>So what is this secret? And can you reuse the same secret for your session? (The answer is &#8220;probably yes,&#8221; but I&#8217;m not 100% sure what the implications are&#8211;since it&#8217;s impossible for end-users to know either or both secret keys.)</p>
<p>Some time ago (in Rails 2.0), they changed the way they handle sessions, so that Rails stores sessions on the <em>client side</em>. Isn&#8217;t this a security risk? Yes! So they implemented this secret-key (the one in environment.rb), so that the <em>client-side session is encrypted</em>&#8211;using the secret key.</p>
<p>&#8220;But wait,&#8221; the security experts cry, &#8220;can&#8217;t you <em>still</em> crack the key by brute-forcing it (by testing every possible combination of characters)? Yes! But <em>given a sufficiently large-enough and convulted enough (i.e. mix of letters and numbers) key, this is computationally infeasible (it&#8217;ll take millions of years to try all the combinations).</em></p>
<p>And <em>that&#8217;s</em> what <code>rake secret</code> is for&#8211;to generate a strong enough, long enough key, to keep your session state safe.</p>
<p>So, again&#8211;Restful Authentication needs a secure key. So generate it with <code>rake secret</code>. It&#8217;s strong enough that you don&#8217;t need to worry about it being cracked. (It generates a 128-character key, there are 1.61 x 10<sup>199</sup> possible combinations&#8211;that&#8217;s with 199 zeros.)</p>
<p><strong>Resources:</strong></p>
<ul>
<li><a href="http://www.rorsecurity.info/2007/11/20/rails-20-cookies/">Rails 2.0 cookies &#8211; Ruby on Rails Security Project</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.railsrocket.com/rake-secret/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>One-Shot Scaffolding Creation</title>
		<link>http://www.railsrocket.com/one-shot-scaffolding-creation</link>
		<comments>http://www.railsrocket.com/one-shot-scaffolding-creation#comments</comments>
		<pubDate>Fri, 09 Jan 2009 21:48:48 +0000</pubDate>
		<dc:creator>Ashiq Alibhai</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[generate]]></category>
		<category><![CDATA[introductory]]></category>
		<category><![CDATA[scaffolding]]></category>

		<guid isPermaLink="false">http://www.railsrocket.com/articles/?p=238</guid>
		<description><![CDATA[If you're not that familiar with Rails, you might be creating your application domain entities (models, controllers, business objects, whatever you want to call them) by generating the model, then generating the controller, then populating the fields into the migration, and the new/edit views. There's a one-line way to do all of this, though. <a href="http://www.railsrocket.com/one-shot-scaffolding-creation">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re not that familiar with Rails, you might be creating your application domain entities (models, controllers, business objects, whatever you want to call them) like this:</p>
<p><code><br />
ruby script/generate model Post<br />
ruby script/generate controller Post<br />
</code></p>
<p>After which, you go in and edit the views for post to create the form with the title, body, etc. All manually.</p>
<p>The great news is that there&#8217;s a shortcut! This shortcut:</p>
<ol>
<li>Creates the model class</li>
<li>Creates the views and controller</li>
<li>Populates the new and edit views <strong>with the class&#8217;s fields</strong></li>
<li>Creates the schema migration <strong>with the class&#8217;s fields</strong></li>
</ol>
<p>How do you do it? Surprisingly easy:</p>
<p><code>ruby script/generate scaffold Post title:string body:text</code></p>
<p>Rails then takes those fields and populates them everywhere necessary. Brilliant! Easy! Use it, it&#8217;ll save you a few minutes of time (which adds up every time you need to create another class)!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.railsrocket.com/one-shot-scaffolding-creation/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Private Member Variables in Ruby</title>
		<link>http://www.railsrocket.com/private-member-variables-in-ruby</link>
		<comments>http://www.railsrocket.com/private-member-variables-in-ruby#comments</comments>
		<pubDate>Wed, 17 Dec 2008 18:49:57 +0000</pubDate>
		<dc:creator>Ashiq Alibhai</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[introductory]]></category>
		<category><![CDATA[object oriented]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[variables]]></category>

		<guid isPermaLink="false">http://www.railsrocket.com/articles/?p=234</guid>
		<description><![CDATA[How can you create private member variables in Ruby? If you're used to the attr_accessor helper, that won't work--that makes your member variables public. You can use the @ notation, eg. @variable_name. (Attr_accessor just gives you free getters and setters.) <a href="http://www.railsrocket.com/private-member-variables-in-ruby">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re new to Ruby, especially if you&#8217;re coming from a Rails background, you probably use attr_accessor (if not outright adding columns to the table in Rails) to add member variables to a class. Like so:</p>
<p><code><br />
class Player<br />
	attr_accessor :x, :y<br />
end<br />
</code></p>
<p>What does this mean? It means you can do things like:</p>
<p><code><br />
p = Player.new<br />
p.x = 2;<br />
p.y += 3;<br />
puts "Player is at (#{p.x}, #{p.y})."<br />
</code></p>
<p>i.e. you get the ability to both read and write to x and y, for FREE&#8211;without any additional code. Smooth!</p>
<p>But, if you&#8217;re like me, you will probably reach a point where you want to add some member variables, but <em>not</em> make them publically accessible. How do you do it?</p>
<p>The answer is to use the <code>@name</code> convention, like so:</p>
<p><code><br />
class Player<br />
	def initialize(x, y)<br />
		@x = x<br />
		@y = y<br />
	end<br />
end<br />
</code></p>
<p>Unlike the attr_accessor code above, this does NOT allow you to do things like:</p>
<p><code><br />
p = Player.new(3, 4)<br />
puts "Player is at #{p.x}, #{p.y}" # raises an error: X and Y don't exist<br />
</code></p>
<p>But, as I mentioned, that&#8217;s what this is for&#8211;creating private member variables.</p>
<p>Also, realize that in Ruby, objects are very dynamic&#8211;you can add properties on the fly! We didn&#8217;t explicitly &#8220;declare&#8221; x and y anywhere in our Player class; yet we can access them.</p>
<p>And that, in a nutshell, is private member variables. One last thing to note&#8211;you can almost think of <code>attr_accessor :some_thing</code> as equivalent to:</p>
<p><code><br />
class Whatever<br />
	@some_thing = nil<br />
	def some_thing<br />
		return @some_thing<br />
	end<br />
	def some_thing=(value)<br />
		@some_thing = value<br />
	end<br />
end<br />
</code></p>
<p>But of course, all this is free by using attr_accessor. So use it!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.railsrocket.com/private-member-variables-in-ruby/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Internationalization (I18n) in Rails 2.2</title>
		<link>http://www.railsrocket.com/internationalization-i18n-in-rails-22</link>
		<comments>http://www.railsrocket.com/internationalization-i18n-in-rails-22#comments</comments>
		<pubDate>Thu, 04 Dec 2008 22:53:49 +0000</pubDate>
		<dc:creator>Ashiq Alibhai</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[I18n]]></category>
		<category><![CDATA[multilingual]]></category>
		<category><![CDATA[Rails 2.2]]></category>

		<guid isPermaLink="false">http://www.railsrocket.com/articles/?p=227</guid>
		<description><![CDATA[Rails 2.2 introduces I18n, aka Internationalization, out-of-the-box. Now, it's very, very easy to write an application that can be translated quickly into another language; all through the new I18n API. You can easily add strings, change strings, even change the language on the fly. We discuss a bit of the evolution of how such a solution came about, which is a common implementation in many programming languages today. <a href="http://www.railsrocket.com/internationalization-i18n-in-rails-22">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Rails 2.2 was released late November, about two weeks ago. Of all the new features, one of the most prominent is the out-of-the-box implementation of internationalization (i18n, because it has 18 letters between I and N) in the new Rails 2.2 generated application (using the <code>rails</code> command).</p>
<p>Before we dive into the implementation, we&#8217;re going to discuss a bit of the evolution of this solution.</p>
<p>In the Good Old Days (possibly before the Internet was widespread in usage), applications hard-coded their strings. So if you had a login form/box/control/screen/something, you&#8217;d have something like:</p>
<p><code><br />
Login:<br />
<input type="text" />
Password:<br />
<input type="password" />
</code></p>
<p>What, then, happens if you want to translate your application into another language like French? You could always hard-code the new values, and maintain a second version; but that&#8217;s expensive to maintain.</p>
<p>Then an idea emerged&#8211;how about storing the strings in a file somewhere, and using <em>some sort of class</em> to get the strings?</p>
<p>So you might have a class like this:</p>
<p><code><br />
class LanguagePack<br />
	def get_string(string_key)<br />
		# open up a text file specified in LanguageManager.language<br />
		# look for the line with string_key<br />
		# return the actual text<br />
	end<br />
end<br />
</code></p>
<p>This class contains the strings. Accompanying this would be the strings file, like so:</p>
<p><code><br />
# en.txt<br />
password = Password:<br />
login = Login:<br />
</code></p>
<p>To obtain strings, you ask the LanguageManager, like so:</p>
<p><code><br />
class LanguageManager</p>
<p>	attr_accessor :language</p>
<p>	def get_string(key)<br />
		LanguagePack.get_string(key)<br />
	end<br />
end<br />
</code></p>
<p>So you can do things like this:</p>
<p><code><br />
LanguageManager.get_string("password") # => "Password: "<br />
LanguageManager.language = "fr" # use French, search fr.txt<br />
LanguageManager.get_string("password") # => "Mot de Passe: "<br />
</code></p>
<p>As you can see, changing languages is easy&#8211;just call <code>LanguageManager.language = fr"</code>, for example, to use French. C&#8217;est bon!</p>
<p>Your form would then be:</p>
<p><code><br />
<%= LanguageManager.get_string(login) %><br />
<input type="text" />
<%= LanguageManager.get_string(password) %><br />
<input type="password" />
</code></p>
<p>And this is precisely how Rails 2.2 handles internationalization! The above form is written as:</p>
<p><code><br />
<%= I18n.t :login %><br />
<input type="text" />
<%= I18n.t :password %><br />
<input type="password" />
</code></p>
<p>Why? Because I18n.t takes a string key, and returns the text, like so:</p>
<p><code><br />
I18n.t("login") => "Login: "<br />
I18n.t("password") => "Password: "<br />
</code></p>
<p>Where are the actual keys and strings stored? They&#8217;re inside <code>config/locales/LANGCODE.yml</code>. For example, English keys (since English is the default language) are stored in en.yml.</p>
<p>When you invoke the <code>rails</code> command to build a new app, by default, en.yml will have these contents:</p>
<p><code><br />
# Sample localization file for English. Add more files in this directory for other locales.<br />
# See http://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.</p>
<p>en:<br />
  hello: "Hello world"<br />
</code></p>
<p>You can easy add new strings; for our input form, we&#8217;d use something like:</p>
<p><code><br />
  login: "Login: "<br />
  password: "Password: "<br />
  # ...<br />
</code></p>
<p>And so on. Also, note that in your <code>environment.rb</code> file, there&#8217;s a new (commented-out) line, like so:</p>
<p><code># config.i18n.default_locale = :de</code></p>
<p>You can uncomment this to set the default locality to German. Or French. Or Japanese. Or Arabic. Or whatever you want!</p>
<p>Also, what about templated strings&#8211;like &#8220;Hello, &lt;your name&gt;?&#8221; The solution is something well-used in different programming languages&#8211;simply use a token in your string, and spcify the value on-the-fly, like so:</p>
<p><code><br />
# in en.yml<br />
  greeting: Hello, {first_name}!</p>
<p># in the code<br />
  <%= I18n.t :greeting, :first_name => "Muhammad" # => Hello, Muhammad!<br />
</code></p>
<p>Finally, how do you change the locale? By specifying <code>I18n.locale</code>, like so:</p>
<p><code>I18n.locale = "de-DE" # German</code></p>
<p>There&#8217;s more to it than this, but we&#8217;ll leave it at this for now. So enjoy!</p>
<p>And don&#8217;t forget the key lesson of this whole shpiel&#8211;<strong><em>internationalize your application!</em></strong> Gone are the days of relying on just one language! The web is too big! (And your users will love you for it!)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.railsrocket.com/internationalization-i18n-in-rails-22/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Rails 2.1: UTC-Timestamped Migrations</title>
		<link>http://www.railsrocket.com/rails-21-utc-timestamped-migrations</link>
		<comments>http://www.railsrocket.com/rails-21-utc-timestamped-migrations#comments</comments>
		<pubDate>Tue, 25 Nov 2008 20:42:20 +0000</pubDate>
		<dc:creator>Ashiq Alibhai</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[migrations]]></category>
		<category><![CDATA[rails 2.1]]></category>

		<guid isPermaLink="false">http://www.railsrocket.com/articles/?p=217</guid>
		<description><![CDATA[Rails 2.1 introduces UTC-timestamped migrations--instead of a single, three-digit prefix to your number, you now get a timestamp (everything from the current year to the current second). This helps you resolve conflicts of the same-numbered migration showing up in multi-developer environments; but it's annoying sometimes! How can you turn this off? Through environment.rb! <a href="http://www.railsrocket.com/rails-21-utc-timestamped-migrations">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Rails 2.1 introduced UTC-timestamped migrations.</p>
<p>What does this mean?</p>
<p>Prior to Rails 2.1, when you added a migration, the number would be a simple three-digit number, like so.</p>
<p><code><br />
ruby script/generate migration create_some_table<br />
=> create db/migrate/008_create_some_table<br />
</code></p>
<p>Imagine, now, you have a multi-developer environment. Jack&#8217;s next task is to create a migration that populates some hello-world data; so he does this:</p>
<p><code><br />
ruby script/generate migration populate_hello_world_data<br />
=> create db/migrate/008_populate_hello_world_data<br />
</code></p>
<p>His local application sees only 7 migrations, so it labels the next migration as 008.</p>
<p>Salman, on the other hand, has another task&#8211;to add created_on and updated_on fields to every single table; so he does this:</p>
<p><code><br />
ruby script/generate migration add_created_and_updated_fields<br />
=> create db/migrate/008_add_created_and_updated_fields<br />
</code></p>
<p>His local application sees only 7 migrations&#8211;Jack didn&#8217;t check his migration in yet! So when both of them are checked in, Rails gets confused&#8211;it sees two migrations numbered 007.</p>
<p>Enter UTC-timestamped migrations. Instead of a number, Rails applies a timestamp, like so:</p>
<p><code><br />
=> create db/migrate/20081121203844_some_migration<br />
</code></p>
<p>Notice the timestamp instead of the number. This is good&#8211;it means unless two developers create a migration at the <strong>exact same second</strong>, they&#8217;ll have different timestamps. (Really, don&#8217;t argue this point&#8211;it&#8217;s easy enough to resolve this if it becomes a conflict, or to shout out &#8220;Incoming migration!&#8221; when you&#8217;re creating a migration.)</p>
<p>Unfortunately, for the rest of us single-developer Rails coders who don&#8217;t <em>need</em> UTC-timestamped migrations (or don&#8217;t know how to specify a specific version&#8211;whatever happened to <code>rake db:migrate version=7</code>?), you can force the old-school migration numbering system, by adding this one line to your environment.rb file:</p>
<p><code>config.active_record.timestamped_migrations = false</code></p>
<p>So enjoy! Use these new timestamped migrations if you need them&#8211;personally, I find the older migration system easier to manage. But I guess it&#8217;s a matter of choice, since I don&#8217;t need to worry about migration conflicts.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.railsrocket.com/rails-21-utc-timestamped-migrations/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Looping Backwards in Ruby</title>
		<link>http://www.railsrocket.com/looping-backwards-in-ruby</link>
		<comments>http://www.railsrocket.com/looping-backwards-in-ruby#comments</comments>
		<pubDate>Wed, 19 Nov 2008 19:18:35 +0000</pubDate>
		<dc:creator>Ashiq Alibhai</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Projects]]></category>

		<guid isPermaLink="false">http://www.railsrocket.com/articles/?p=207</guid>
		<description><![CDATA[In Ruby, looping or iterating forwards is a simple task--you can use the bracket notation. But what if you want to loop backwards? Can you do it? Easily? Or what if you don't know if you're iterating forwards or backwards? Is there a generic solution you can use for this specific, albeit rare, case? <a href="http://www.railsrocket.com/looping-backwards-in-ruby">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>It so happens in Ruby that looping forwards is trivial; if you want to print out numbers from one to five, you can do so like this:</p>
<p><code><br />
(1..5).each do |i|<br />
	puts i<br />
end<br />
</code></p>
<p>This will print:<br />
<code><br />
1<br />
2<br />
3<br />
4<br />
5<br />
</code></p>
<p>But how about going backwards? Can you do this?</p>
<p><code><br />
(5..1).each do |i|<br />
	puts i<br />
end<br />
</code></p>
<p>No! This won&#8217;t print <em>anything</em> out! Boo!</p>
<p><strong>Edit:</strong> The solution is, happily, as easy as:<br />
<code><br />
5.downto(1).do |i|<br />
	puts i<br />
end<br />
</code></p>
<p>Props out to Wayne Conrad for mentioning it in the comments! In any case, you can read on to see a discussion of some other solutions to this problem&#8211;solutions that will work if you <em>don&#8217;t know if you&#8217;re going to be counting up or counting down</em>.</p>
<hr />
<p>Ok, so you might do this, instead:</p>
<p><code><br />
count = 5<br />
while count > 0<br />
	puts count<br />
	count -= 1<br />
end<br />
</code></p>
<p>Ok, that&#8217;ll work. But what if you have two variables, x and y, and you want to iterate from x and y&#8211;except you never know if x is less than y or greater than y? (If x is less than y, you can use the usual <code>(x..y).each do |i|</code>, but if not, what then?)</p>
<p>There are a couple of ways you can tackle this&#8211;one is <strong>swapping</strong>, and the other is <strong>using min/max</strong>, and we discuss the <strong>reverse series</strong> method.</p>
<p><strong>Swapping</strong> means swapping x and y if x > y, like so:</p>
<p><code><br />
x = #...<br />
y = #...</p>
<p>if (x > y)<br />
	temp = x<br />
	x = y<br />
	y = temp<br />
end</p>
<p>(x..y).each do #...<br />
</code></p>
<p>This is a great solution. Sometimes, though, it doesn&#8217;t work&#8211;like in my case, I had an issue where I go from x to y, and then from y to some other value. Swapping just destroys the second part of the process.</p>
<p>Which brings us to solution two&#8211;<strong>using min/max functions</strong> (which, incidentally, don&#8217;t come built-in to Ruby). So you would write:</p>
<p><code><br />
(min(x, y)..max(x, y)).each do #...<br />
</code></p>
<p>And this will iterate properly, without destroying the values of x and y. Of course, you can define min/max as:</p>
<p><code><br />
def min(a, b)<br />
	return b unless a < b<br />
	return a<br />
end<br />
</code></p>
<p><code><br />
def max(a, b)<br />
	return b unless a > b<br />
	return a<br />
end<br />
</code></p>
<p>And you can use this solution. AND, if you don't know if x is greater than or less than y, then this solution will STILL work! Yay!</p>
<p>BUT! this solution <em>still</em> iterates forward! If that's a problem, you can try solution three, the <strong>reverse series</strong>. Observe:</p>
<p><code><br />
steps = y - x<br />
while (steps > 0)<br />
	i = y - steps #(y, y-1, y-2 ... x)<br />
	# do something for step i<br />
end<br />
</code></p>
<p>This iterates backwards; the value of i is y, then y-1, then y-2, and so on, up to x. (Or, as mentioned before, you can use downto.)</p>
<p>Phew! So that covers a few different ways of iterating backwards; use whatever works for your needs.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.railsrocket.com/looping-backwards-in-ruby/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>The Controversial Eval Function</title>
		<link>http://www.railsrocket.com/the-controversial-eval-function</link>
		<comments>http://www.railsrocket.com/the-controversial-eval-function#comments</comments>
		<pubDate>Tue, 11 Nov 2008 20:15:28 +0000</pubDate>
		<dc:creator>Ashiq Alibhai</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[dangerous code]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[reflection]]></category>

		<guid isPermaLink="false">http://www.railsrocket.com/articles/?p=200</guid>
		<description><![CDATA[eval is a function in Ruby that allows you to execute arbitrary code. You pass in a string containing code, and voila! It's very useful, for things like holding pointers of a sort to functions; but it's also dangerous, because script-kiddies can format your hard-drive. So what's the middle-path use-case of eval? <a href="http://www.railsrocket.com/the-controversial-eval-function">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Today we&#8217;re going to discuss one of those highly-controversial hot topics in Ruby (if there can <em>be</em> such a thing as a &#8220;hot topic&#8221; in programming languages)&#8211;the <code>eval</code> function.</p>
<p>What does <code>eval</code> do? Quite simply, you pass in some Ruby code in a string, and it&#8217;ll evaluate it. Observe:</p>
<p><code><br />
eval "puts 2+2" # => 4<br />
eval "'hello world!'.upcase # => HELLO WORLD<br />
</code></p>
<p>Clearly, this is useful stuff&#8211;you can pass in arbitrary code, even assemble your strings on the fly for <em>truly</em> complicated code.</p>
<p>But, eval is a dangerous tool&#8211;because it can execute <em>arbitrary</em> code. For example, if you have some code like this:</p>
<p><code><br />
input = # ... read from some form<br />
eval input<br />
# ...<br />
</code></p>
<p>Some clever script kiddie will send the input <code>system 'rm -rf *'</code>, you can kiss your application goodbye. And your family photo-collection. And your MP3 collection. And your hard-drive.</p>
<p>Some people brand eval as an &#8220;evil,&#8221; &#8220;insidious,&#8221; &#8220;destructive&#8221; function. Which is true&#8211;it <em>has</em> destructive potential. That&#8217;s why you need to <em>sanitize</em> your input. Don&#8217;t just send it as-is. Don&#8217;t risk it.</p>
<p>On the other hand, eval has a lot of tricky uses for things you might not otherwise be able to do. For example, how would you create a generic &#8220;shutdown hook&#8221; system for your Ruby application? other programming languages have pointers&#8211;in .NET, for example, you can accumulate a collection of pointers, and run those. What can you do in Ruby? You can accumulate a collection of strings to eval!</p>
<p><code><br />
shutdown_hooks = ["Logger.write 'normal termination'", "Logger.close", "DatabaseConnector.close"]<br />
</code></p>
<p>Voila! You can iterate through them and execute them, just like any set of pointers in other languages.</p>
<p>To recap:</p>
<ul>
<li>eval is merely a tool. It can be used, and abused, like any tool.</li>
<li>Use eval as necessary, but avoid it if possible.</li>
<li><em>Sanitize</em> your input! Don&#8217;t risk it! Script kiddies abound!
</ul>
<p>In other programming languages, one of the concerns raised with reflection is that it&#8217;s a &#8220;heavy&#8221; process. How does Ruby stack up? If you know, share it in the comments!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.railsrocket.com/the-controversial-eval-function/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Block Comments in Ruby</title>
		<link>http://www.railsrocket.com/block-comments-in-ruby</link>
		<comments>http://www.railsrocket.com/block-comments-in-ruby#comments</comments>
		<pubDate>Wed, 05 Nov 2008 22:22:40 +0000</pubDate>
		<dc:creator>Ashiq Alibhai</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[comments]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[whitespace]]></category>

		<guid isPermaLink="false">http://www.railsrocket.com/articles/?p=196</guid>
		<description><![CDATA[How do you comment out a chunk of code? Other programming languages (C++, Java, etc.) have a slash-star style of block comments, like so: /* */ ... but what about Ruby? Does Ruby have a mechanism for this, too? The answer is YES; BUT, there's a catch! If you don't watch whitespace ... <a href="http://www.railsrocket.com/block-comments-in-ruby">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>This is an easy one. How do you comment out a chunk of code? Other programming languages (C++, Java, etc.) have a slash-star style of block comments, like so:</p>
<p><code><br />
/*<br />
	This is a block comment. You can put whatever you want here.<br />
	Even ASCII art! For REAL!  o[+++]XXXXXXXXXXXX><br />
*/<br />
	// ...<br />
</code></p>
<p>But what about Ruby?</p>
<p>Ruby has two special tags that you use: <code>=begin</code> and <code>=end</code>. So your Ruby comment would look like this:</p>
<p><code><br />
=begin<br />
	This function returns the minimum of A or B.<br />
	For example, if A is 3 and B is 7, it'll return 3.<br />
=end<br />
def self.min(a, b)<br />
	# ...<br />
end<br />
</code></p>
<p>Yes, yes! It&#8217;s easy!</p>
<p>Wait! What&#8217;s that, you say? You say it <em>doesn&#8217;t work</em>? Well, that&#8217;s tragic&#8211;and in fact, it happened to me, too. The key is that <strong>these tokens cannot be preceded by whitespace.</strong> If they are, it won&#8217;t work.</p>
<p>What? Ghetto, you say? Well, tough luck, my friend&#8211;this is how Ruby deals with block comments. Just use it, OK? It&#8217;s easier than commenting out 130+ lines of code one by one. (Or if you have an IDE that does it in one-shot, great, even better!)</p>
<p>And that&#8217;s Ruby comment blocks! Simple!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.railsrocket.com/block-comments-in-ruby/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

