Newbie here, having a hard time understanding Class methods and why I cannot get an attribute to show up correctly in the instance.
class Animal
attr_accessor :noise, :color, :legs, :arms
def self.create_with_attributes(noise, color)
animal = self.new(noise)
@noise = noise
@color = color
return animal
end
def initialize(noise, legs=4, arms=0)
@noise = noise
@legs = legs
@arms = arms
puts "----A new animal has been instantiated.----"
end
end
animal1 = Animal.new("Moo!", 4, 0)
puts animal1.noise
animal1.color = "black"
puts animal1.color
puts animal1.legs
puts animal1.arms
puts
animal2 = Animal.create_with_attributes("Quack", "white")
puts animal2.noise
puts animal2.color
When I use the class method create_with_attributes (on animal.2), I expect "white" to appear when I puts animal2.color.
It seems as though I have defined it using attr_accessor just like I have "noise", and yet noise appears correctly while color will not. I do not get an error when I run this program, but the .color attribute is just not appearing. I believe it is because I have somehow labeled it incorrectly in the code.