Module: LinkedList::Conversions

Included in:
List
Defined in:
lib/linked-list/conversions.rb

Class Method Summary collapse

Class Method Details

.List(arg) ⇒ Object

List() tries to conver its argument to List object by first calling #to_list, if that is not availabe and its argument is an array (or can be convertd into array with #to_ary) then it will instantiate a new List object making nodes from array elements. If none above applies, then a new List will be instantiated with one node holding argument value.

Returns:

New List object.



32
33
34
35
36
37
38
39
40
# File 'lib/linked-list/conversions.rb', line 32

def List(arg)
  if arg.respond_to?(:to_list)
    arg.to_list
  elsif arg.respond_to?(:to_ary)
    arg.to_ary.each_with_object(List.new) { |n, l| l.push(Node(n)) }
  else
    List.new.push(Node(arg))
  end
end

.Node(arg) ⇒ Object

Node() tries to convert its argument to Node object by first calling #to_node, if that is not availabe then it will instantiate a new Node object.

Returns:

New Node object.



14
15
16
17
18
19
20
# File 'lib/linked-list/conversions.rb', line 14

def Node(arg)
  if arg.respond_to?(:to_node)
    arg.to_node
  else
    Node.new(arg)
  end
end