Skip to content

Instantly share code, notes, and snippets.

@neilmock
Created April 21, 2010 11:36
Show Gist options
  • Save neilmock/373726 to your computer and use it in GitHub Desktop.
Save neilmock/373726 to your computer and use it in GitHub Desktop.

Ruby

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.

Language Overview

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.

Arrays and Hashes (Data Structures)

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

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'

Control Structures

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 

Blocks and Iterators

Coming soon...

For Windows

Docs, Tutorials, Resources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment