Class: LinkedList::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/linked_list/node.rb

Overview

Implements a single node of the linked list.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, cdr) ⇒ Node

Returns a new instance of Node.



6
7
8
# File 'lib/linked_list/node.rb', line 6

def initialize(value, cdr)
  @cdr, @value = cdr, value
end

Instance Attribute Details

#cdrObject

Returns the value of attribute cdr.



4
5
6
# File 'lib/linked_list/node.rb', line 4

def cdr
  @cdr
end

#valueObject

Returns the value of attribute value.



4
5
6
# File 'lib/linked_list/node.rb', line 4

def value
  @value
end

Instance Method Details

#==(other) ⇒ Object

Two nodes are equal if their values are equal and their cdr’s are equal too.



11
12
13
# File 'lib/linked_list/node.rb', line 11

def ==(other)
  cdr == other.cdr && value == other.value
end

#inspectObject

Returns a nice-looking string.

node = LinkedList::Node.new("a", nil)
node.inspect
#=> ("a" nil)


19
20
21
# File 'lib/linked_list/node.rb', line 19

def inspect
  "(#{value.inspect} #{cdr.inspect})"
end