title: Modify Calling Object in Ruby
body: <p>I am in the process of learning Ruby and as practice I am making a linked list class.  I am in the process of writing the delete method for a doubly linked list.  My question is, if I represent the list by its head node, how do I delete the head?  It seems like Ruby wont allow you to assign to the self variable, so I can't change the reference of the caller to be the next node.  One solution is that I can copy the key from the next node and swap references, but in general, is there a way in Ruby to change the reference of the caller?</p>
  
  <pre><code>class LinkedListNode
  
      attr_accessor :next, :previous, :key
  
      def initialize(key=nil, next_node=nil, previous=nil)
          @next = next_node
          @previous = previous
          @key = key
      end
  
      def append(key=nil)
          newnode = LinkedListNode.new(key)
          seeker = self
          while seeker.next != nil
             seeker = seeker.next
          end
          newnode.previous = seeker
          seeker.next = newnode
       end
  
       def delete(key=nil)
           seeker = self
           while seeker.key != key
               return if seeker.next == nil
               seeker = seeker.next
           end
           if seeker.previous != nil
              if seeker.next != nil
                  seeker.previous.next = seeker.next
                  seeker.next.previous = seeker.previous
              else
                  seeker.previous.next = nil
              end
           else
               return self = self.next
           end
           return seeker = nil
       end
  
       def print
           seeker = self
           string = ""
           while 1
              if seeker.next == nil
                  string += seeker.key.to_s
                  break
              else
                  string += seeker.key.to_s + " -&gt; "
              end
              seeker = seeker.next
          end
          puts string
      end
  end
  
  if __FILE__ == $0
      ll = LinkedListNode.new(1)
      ll.append(2)
      ll.append(3)
      ll.append(4)
      ll.append(5)
  
      ll.print
  
      ll.delete(5)
      ll.print
  
      ll.delete(1)
      ll.print
  end
  </code></pre>