i understand in ruby, attr_accessor
key creates getter/setter
methods. example,
attr_accessor :data
def data=(value) @data = value end def data @data end
so, if case, node
class why need create initialize
method?
ex:
def initialize(data, next_node) @data = data; @next_node = next_node;
couldn't write att_accessor :data, next_node
?
with initialize
method can write:
node = node.new('foo', node)
without (and attr_accessor
) need write:
node = node.new node.data = 'foo' node.next_node = node
to answer question: there no need initialize
method, more convenient use.
Comments
Post a Comment