Advertisement
Guest User

Untitled

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