Stacklikeness means adding items at one end and removing the most recently added item first: last in, first out (LIFO). section 4.1.1
In Ruby, stacklike behavior belongs in a module when multiple kinds of objects may need the same operations. A module can then be mixed into classes that require that behavior. section 4.1.1
A Ruby stacklike module
The Stacklike module uses an array stored in @stack, exposing methods to add objects with push and remove the newest object with pop. section 4.1.1
Listing 4.1 Stacklike module, encapsulating stacklike structure and behavior
module Stacklike
def stack
@stack ||= [] 1
end
def add_to_stack(obj) 2
stack.push(obj)
end
def take_from_stack 3
stack.pop
ends
end
Inspect the compact module definition and its stack, add, and remove operations. section 4.1.1
