I'm new to ruby but trying to learn. I've been trying to figure out some
things about a book example and having the darndest time of it. I've
been trying to google and look up the api and use a book reference but
I'm left with some questions. That's where I was hoping a kind soul from
this forum could come in. Answers directly below, code on bottom.

I understand that @varname is essentially defining how open the variable
"varname" is, but I don't understand why "@children = children" is
needed, couldn't the code just use "children"?

children and node_name reference attr_accessor. Am I right to think that
the names of these two objects are inconsequential? What are we doing
here? I'm guessing making instances of attr_accessor and naming them
"children" and "node_name"?

Does children have the "each" parameter by virtue of inheriting from
attr_accessor? Is each something I can do on each and every object then?

thanks for bearing with me!



class Tree
  attr_accessor :children,  :node_name

  def initialize (name, children=[])
    @children = children
    @node_name = name
  end

  def visit_all(&block)
    visit &block
    children.each {|c| c.visit_all &block}
  end


  def visit(&block)
    block.call self
  end
end

ruby_tree = Tree.new( "Ruby", [Tree.new("Reia"), Tree.new("MacRuby")])

puts "Visiting a node"
ruby_tree.visit {|node| puts node.node_name}
puts

puts "Visiting entire tree"
ruby_tree.visit_all {|node| puts node.node_name}

-- 
Posted via http://www.ruby-forum.com/.