Ruby is a dynamic, truly object-oriented language (everything is an object) that is also influenced in many ways from concepts found in functional programming. It open source, and was first released in 1995 by Yukihiro “matz” Matsumoto. The most current version is 1.9.1.
Here's a sample Ruby method to get started with syntax:
def say_goodnight(name)
result = "Good night, " + name
return result
end
>> puts say_goodnight("John-Boy") # puts outputs its argument followed by a newline
=> Good night, John-Boy
Ruby syntax is fairly clean. You don’t need semicolons at the ends of statements as long as you put each statement on a separate line. Ruby comments start with a "#" character and run to the end of the line. Code layout is pretty much up to you; indentation is not significant but 2-character indentation is the norm in the community.
Methods are defined with the keyword def, followed by the method name (in this case, say_goodnight) and the method’s parameters between parentheses. (In fact, the parentheses are optional, but we like to use them.) Ruby doesn’t use braces to delimit the bodies of compound statements and definitions. Instead, you simply finish the body with the keyword end.
Ruby’s arrays and hashes are indexed collections. Both store collections of objects, accessible using a key. With arrays, the key is an integer, whereas hashes support any object as a key. Both arrays and hashes grow as needed to hold new elements.
Here's an array example:
>> a = [ 1, 'cat', 3.14 ] # array with three elements
>> puts "The first element is #{a[0]}"
=> The first element is 1
>> a[2] = nil # set the third element
>> puts "The array is now #{a.inspect}"
=> The array is now [1, "cat", nil]
And here's a hash example:
fruits = {
'apple' => 'red',
'orange' => 'orange',
}
>> puts fruits['apple']
=> 'red'
A hash by default returns nil when indexed by a key it doesn’t contain. In many languages, the concept of nil (or null) means “no object.” In Ruby, that’s not the case; nil is an object, just like any other, that happens to represent nothing.
Symbols are simply constant names that you don’t have to predeclare and that are guaranteed to be unique. A symbol literal starts with a colon and is normally followed by some kind of name:
>> :north
>> :east
Ruby guarantees that no matter where it appears in your program, a particular symbol will have the same value. That is, you can write the following:
def walk(direction)
if direction == :north
# ...
end
end
Symbols are frequently used as keys in hashes. We could write the previous hash example as this:
fruits = {
:apple => 'red',
:orange => 'orange',
}
>> puts fruits[:apple]
=> 'red'
Ruby has all the usual control structures, such as if statements and while loops. There are no braces around the bodies of these statements. Instead, Ruby uses the keyword end to signify the end of a body:
if count > 10
puts "Try again"
elsif tries == 3
puts "You lose"
else
puts "Enter a number"
end
Similarly, while statements are terminated with end:
while weight < 100 and num_pallets <= 30
pallet = next_pallet()
weight += pallet.weight
num_pallets += 1
end
Ruby statement modifiers are a useful shortcut if the body of an if or while statement is just a single expression. Simply write the expression, followed by if or while and the condition. For example, here’s a simple if statement:
if radiation > 3000
puts "Danger, Will Robinson"
end
Here it is again, rewritten using a statement modifier:
puts "Danger, Will Robinson" if radiation > 3000
Similarly, a while loop such as this:
square = 2
while square < 1000
square = square*square
end
becomes this more concise version:
square = 2
square = square*square while square < 1000
Coming soon...
- Ruby Windows Installer: http://rubyinstaller.org/
- RubyMine Windows IDE: http://www.jetbrains.com/ruby/download/index.html#win
- Ruby Language Docs & Tutorials: http://www.ruby-lang.org/en/documentation/
- Ruby Koans: http://github.com/edgecase/ruby_koans (can checkout SVN using this URL: http://svn.github.com/edgecase/ruby_koans)
- Interactive Ruby in the browser: http://tryruby.org/
- Ruby Users Guide (unofficial) http://www.rubyist.net/~slagell/ruby/index.html