Class: ImmutableStack
- Inherits:
-
Object
- Object
- ImmutableStack
- Defined in:
- lib/ImmutableStack.rb
Class Method Summary collapse
Instance Method Summary collapse
- #each {|head| ... } ⇒ Object
-
#initialize(h, t) ⇒ ImmutableStack
constructor
A new instance of ImmutableStack.
- #pop ⇒ Object
- #push(element) ⇒ Object
Constructor Details
#initialize(h, t) ⇒ ImmutableStack
Returns a new instance of ImmutableStack.
12 13 14 15 |
# File 'lib/ImmutableStack.rb', line 12 def initialize(h,t) @head = h @tail = t self.freeze end |
Class Method Details
.empty ⇒ Object
4 |
# File 'lib/ImmutableStack.rb', line 4 def self.empty() @empty ||= self.new(nil, nil) end |
Instance Method Details
#each {|head| ... } ⇒ Object
5 6 7 8 9 10 11 |
# File 'lib/ImmutableStack.rb', line 5 def each() yield(head) t = tail while t.!=(ImmutableStack.empty) do h, t = t.pop yield(h) end end |
#pop ⇒ Object
2 |
# File 'lib/ImmutableStack.rb', line 2 def pop() [@head, @tail] end |
#push(element) ⇒ Object
3 |
# File 'lib/ImmutableStack.rb', line 3 def push(element) ImmutableStack.new(element, self) end |