Scott Weisman

A Solid Ruby Foundation - Code Academy Week 2

April 22, 2012

I've been working hard for the first two weeks of Code Academy to build a solid foundation with Ruby. As I mentioned in my last post, when you work with Rails it can be easy to forget the magic that Ruby is doing behind the scenes.

To make sure that I don't forget the basics of Ruby, I've outlined them over and over. Below is my most basic outline of Ruby concepts that I never want to forget. Some of the examples are taken from the excellent book Learn to Program by Chris Pine.

Arrays and Hashes


Create new array and call 0 index:

a = ["scott", "mike", "joe"]
a[0]    # => "scott"

Add to array:

a << "mark"   #or
a[3] = "mark"


Create new hash using symbol for key and call:

h = { :dog => "woof", "cat" => "meow" }
h[:dog]    # => "woof"

Add to hash:

h[:cow] = "moo"


Each Method (p. 66)

languages.each do |lang|
  puts "I love " + lang

Integer Method (p. 67)

3.times do
  puts 'hip-hip-hooray!'


Methods with parameters (p. 76)

def say_moo(number_of_moos)
  puts 'mooooo...'*(number_of_moos)

say_moo(3)   # This is the method call with parameter

Initialize method is called automatically when new instance of class is created (p. 132)


Built in Classes (p. 117)

Typically, you just create new built in classes by setting values:

a = [1, 2, 3]

But you are actually calling when you do this:

a = + [1, 2, 3]