Advertisement
Guest User

Untitled

a guest
Sep 24th, 2012
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.87 KB | None | 0 0
  1. class Animal
  2.     attr_accessor :name
  3.     attr_writer :color
  4.     attr_reader :legs, :arms
  5.  
  6.     def self.all_species
  7.         ['cat', 'cow', 'dog', 'duck', 'horse', 'pig']
  8.     end
  9.  
  10.     def self.create_with_attributes (noise, color)
  11.         animal = Animal.new(noise) # or use animal = self.new(noise)
  12.         animal.color = color
  13.         return animal
  14.     end
  15.  
  16.     def initialize(noise, legs=4, arms=0)
  17.         @noise = noise
  18.         @legs = legs
  19.         @arms = arms
  20.         puts "A new animal has been instantiated."
  21.     end
  22.     def noise=(noise)
  23.         @noise = noise
  24.     end
  25.  
  26.     def noise
  27.         @noise
  28.     end
  29.  
  30.     def color
  31.         "The color is #{@color}"
  32.     end
  33.  
  34. end
  35.  
  36. puts Animal.all_species.inspect
  37.  
  38. animal1 = Animal.new("mooo", 5, 0)
  39. animal1.name = "Steve"
  40. puts animal1.name
  41. animal1.color = "Black"
  42. puts animal1.color
  43. puts animal1.noise
  44.  
  45. puts ''
  46.  
  47. animal2 = Animal.create_with_attributes("Quack!", "white")
  48. puts animal2.noise
  49. puts animal2.color
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement