title: Inserting a value into a linked list in Ruby
body: <p>I am having trouble using linked list in Ruby. In the method <code>insert_node</code>, if <code>head</code> is not <code>nil</code> then <code>current_node.next = _node</code> will get the value to be inserted at the end of the linked list. I don't understand how <code>head</code> is being updated with the value added to the end of the linked list. To me it seems that <code>current_node</code> is a copy of <code>head</code> and then after the until loop <code>current_node.next</code> gets the value to be inserted. Just by looking at it I thought that <code>head</code> will be the same previous linked list without the added value assuming head is not <code>nil</code>.</p>
  
  <pre><code>class Node 
    attr_accessor :data, :next
  
    def initialize(data)
      @data = data 
      @next = nil 
    end
  end
  
  class List
  
    def insert_node(head, value)
      _node = Node.new(value)
  
      if head.nil?
        return _node
      end
  
      current_node = head 
      until current_node.next.nil?
        current_node = current_node.next
      end
  
      current_node.next = _node
      head
    end
  
    def display(head)
      temp = head 
      while temp 
        print "#{temp.data} "
        temp = temp.next
      end
    end
  
  end
  
  obj = List.new
  head = nil
  
  head = obj.insert_node(head, 1)
  head = obj.insert_node(head, 2)
  obj.display(head)
  </code></pre>