Class: Cell

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/nendo.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(car = Nil.new, cdr = Nil.new) ⇒ Cell

Returns a new instance of Cell.



49
50
51
52
# File 'lib/nendo.rb', line 49

def initialize( car = Nil.new, cdr = Nil.new )
  @car = car
  @cdr = cdr
end

Instance Attribute Details

#carObject

Returns the value of attribute car.



53
54
55
# File 'lib/nendo.rb', line 53

def car
  @car
end

#cdrObject

Returns the value of attribute cdr.



53
54
55
# File 'lib/nendo.rb', line 53

def cdr
  @cdr
end

Instance Method Details

#eachObject

Supporting iterator



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/nendo.rb', line 55

def each                    # Supporting iterator
  if not isNull
    it = self
    while Nil != it.class
      yield it
      if it.cdr.is_a? Cell
        it = it.cdr
      else
        it = Nil.new
      end
    end
  end
end

#isDottedObject



72
73
74
# File 'lib/nendo.rb', line 72

def isDotted
  ((Cell != @cdr.class) and (Nil != @cdr.class))
end

#isNullObject



76
77
78
# File 'lib/nendo.rb', line 76

def isNull
  ((Nil  == @car.class) and (Nil == @cdr.class))
end

#lastAtomObject



86
87
88
89
90
91
92
93
# File 'lib/nendo.rb', line 86

def lastAtom
  lastOne = self.lastCell
  if lastOne.isDotted
    lastOne.cdr
  else
    nil
  end
end

#lastCellObject



80
81
82
83
84
# File 'lib/nendo.rb', line 80

def lastCell
  lastOne = self
  self.each { |x| lastOne = x }
  lastOne
end

#lengthObject



69
# File 'lib/nendo.rb', line 69

def length() self.to_arr.length  end

#sizeObject

alias of length



70
# File 'lib/nendo.rb', line 70

def size()   self.length         end

#to_arrObject



95
96
97
98
99
100
101
# File 'lib/nendo.rb', line 95

def to_arr
  if isNull
    []
  else
    self.map {|x| x.car}
  end
end