1

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.

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks so much for your help. I was banging my head against the wall and you made it very clear for me. Much appreciated!!!
If I'm understanding you correctly, than the reason the noise attribute was appearing in animal.2 is because I had set it in the Initialize method, which was setting the instance variable?
Right. Because your initialize method requires the noise, the animal.noise = noise line in self.create_with_attributes isn't actually necessary, as it will already be set.
I know this is a very beginner question, but can you explain the significance of putting return animal in the self.create_with.attributes method? What is this actually doing?
Sure. In Ruby, everything is an expression, and expressions return the last thing that was evaluated in them automatically. Having an explicit return at the end of a method doesn't break anything, but is redundant, because a method will already return the last expression that was evaluated within it. You do need an explicit return in the case you want to return from a method early, but otherwise is considered 'unrubyish'. Take a look at a contrived example I put up at gist.github.com/a29e73325cd42eaeb16d which may help a bit.
1

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.

1 Comment

Thanks for adding the very last note about removing the setting of "noise". That made it very clear to me what was going on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.