Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Sep 20th, 2012  |  syntax: None  |  size: 0.54 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. class Person
  2.  
  3.   def speak
  4.     puts "My name is #{@name}. I am a #{@occupation}."
  5.   end
  6.  
  7.   def occupation(occupation)
  8.     @occupation = occupation
  9.   end
  10.  
  11.   def initialize(name)
  12.     @name = name
  13.   end
  14. end
  15.  
  16. class House
  17.  
  18.   def residents
  19.     @people.each do |person|
  20.       person.speak
  21.     end
  22.   end
  23.  
  24.   def move_in(person)
  25.     @people.push(person)
  26.   end
  27.  
  28.   def initialize
  29.     @people = Array.new
  30.   end
  31. end
  32.  
  33. # examples of how the above code works:
  34. man = Person.new("Fred")
  35. man.occupation("Garbage man")
  36.  
  37. home = House.new
  38. home.move_in(man)
  39.  
  40. home.residents