background preloader

Ruby

Facebook Twitter

Graphics

Bienvenue. Mr. Neighborly's Humble Little Ruby Book. Now that you are familiar with some basic objects and how to manipulate them a little bit, we should probably move on to segmenting your code; surely you didn't think that applications ran in one huge chunk of code!

Mr. Neighborly's Humble Little Ruby Book

It would be silly if you had to repeat every piece of code you wanted to use again or worry constantly about if you were stomping on a variable you used 30,000 lines ago, so the language geniuses have created a few ways to segment your code. The most fundamental of these is a block; blocks are just pieces of code segmented away from their context by an initiator and the end keyword. The initiator could be the begin keyword (which I will use as an example here) or something like if or for (which you will learn about in the next section).

Begin puts "I'm in a block. " puts "Still there... " puts "Stillllll in here... " puts "OK, I'm done. " end Methods. Learn Ruby with the EdgeCase Ruby Koans. Ruby Tutorial - Learn Ruby. Code School - TryRuby.