Hi guys I have a question on adding methods to instance objects. There is something I don't get.
I'm trying to do a binary search tree in Ruby. So I made a node class like such:
class Node
attr_accessor :value, :right, :left
def initialize(value)
@value = value
end
end
So when I want to create an instance object from that class I do this:
tree = Node.new(10)
But if I want to create a binary search tree I need left and right pointers for values that are less and higher than my root number.
So that should go something like this.
tree.left = Node.new(8)
tree.right = Node.new(13)
And if I want to go further I do this:
tree.left.left = Node.new(7)
And from my inspect method that I've adapted i get this:
"{10::{8::{6::nil|nil}|nil}|nil}"
But from this:
left=Node.new(1).left = Node.new(2).left = Node.new(3)
I get:
{3::nil|nil}
So, why doesn't this method chaining work like on the previous example?
Thanks