Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.89 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 = 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.  
  23.   def noise=(noise)
  24.     @noise = noise
  25.   end
  26.  
  27.   def noise
  28.     @noise
  29.   end
  30.  
  31.   def color
  32.     "The color is #{@color}."
  33.   end
  34. end
  35.  
  36. puts Animal.all_species.inspect
  37.  
  38. animal1 = Animal.new("Moo!", 4, 0)
  39. animal1.name = "Steve"
  40. puts animal1.name
  41. animal1.color = "black"
  42. puts animal1.color
  43. puts animal1.legs
  44. puts animal1.noise
  45.  
  46. animal2 = Animal.create_with_attributes("Quack!", "white")
  47. puts animal2.noise
  48. puts animal2.color
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement