Class methods (ruby)

2019-06-03 06:03发布

问题:

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.

回答1:

self.create_with_attributes is a class method, so setting @noise and @color within it is not setting an instance variable, but instead what's known as a class instance variable.

What you want to do is set the variables on the instance you've just created, so instead, change self.create_with_attributes to look something like:

 def self.create_with_attributes(noise, color)
     animal = self.new(noise)
     animal.noise = noise
     animal.color = color
     animal
 end

which will set the attributes on your new instance, instead of on the class itself.



回答2:

When you're in the create_with_attributes method, the instance variables are set on the Animal class itself, not on the instance of Animal you've just created. This is because the method is on the Animal class (which is an instance of Class), and thus it is run within that context, not the context of any instance of Animal. If you do:

Animal.instance_variable_get(:@color)

after running the method like you describe, you should get "white" back.

That said, you need to instead set the attributes on the instance you've just created by calling the setter methods like so:

def self.create_with_attributes(noise, color)
  animal = self.new(noise)
  animal.color = color
  return animal
end

I removed the setting of noise since it's done in your initialize anyway.