0

在我展开我的问题之前,让我声明我已经阅读了这里这里这里的问题的答案。因此,要求您不要将我的问题标记为与那些答案一样重复并没有让我理解attr_accessor. 我的问题更多与逻辑有关,而不是语法。

我在下面创建了两组代码。这些集合彼此相同,只是其中一个集合根本没有attr_accessor线。当我运行两组时,它们都给了我相同的输出。attr_accessor那么,从逻辑上讲,当两组代码给我相同的预期输出时,这行代码有什么区别?

代码集 1:

class Animal 

   def initialize(name)
      @name = name
   end
end
class Cat < Animal
   def talk
     "Meaow!"
   end
end
class Dog < Animal
   def talk
     "Woof!"
   end
end

animals = [Cat.new("Flossie"), Dog.new("Clive"), Cat.new("Max")]
animals.each do |animal|
    puts animal.talk
end
#Output:
#Meaow!
#Woof!
#Meaow!

代码集 2:

class Animal

attr_accessor :name  #this line is the only difference between the two code sets.

   def initialize(name)
      @name = name
   end
end
class Cat < Animal
   def talk
     "Meaow!"
   end
end
class Dog < Animal
   def talk
     "Woof!"
   end
end

animals = [Cat.new("Flossie"), Dog.new("Clive"), Cat.new("Max")]
animals.each do |animal|
    puts animal.talk
end
#Output:
#Meaow!
#Woof!
#Meaow!

Both sets of code call the Animal class to create new instances of animal objects WITH names. I stress on "...WITH names." because the attr_accessor (in the 2nd set) is defining the :name attribute. But in the 1st code set, I have deleted the attr_accessor but still managed to create object instances with the name attribute.

4

2 回答 2

1

attr_accessor :attribute_name is shorthand for:

def attribute_name
  @attribute_name
end

def attribute_name=(value)
  @attribute_name = value
end

and it's for setting instance variable. In your code snipped, you set instance variable directly in initialize method, so you don't need attr_accessor.

于 2013-08-21T13:16:31.327 回答
0

Instance variables can always be read/written inside instance methods, which your code demonstrates. attr_accessor makes instance variables readable/writable outside the class (by defining accessor methods). By adding it to your second example, you allow the following:

cat = Cat.new("Garfield")
puts cat.name
cat.name = "Maru"

which would raise NoMethodError in your first example.

于 2013-08-21T14:32:20.683 回答