Blow Your Stack : Push « Array « Ruby






Blow Your Stack


# A stack is a LIFO (last in, first out) structure. 
# You can use an array like a stack by using the push and pop methods from the Array class. 

fruit = %w[ apple orange banana ]
fruit.pop # => "banana"
p fruit # => ["apple", "orange" ]
fruit.push "mango"
p fruit # => ["apple", "orange", "mango"]

 








Related examples in the same category

1.You can add things to the end of the array by pushing data into it
2.With arrays, << is the operator for pushing an item onto the end of an array.
3.Push integer and string to the same array
4.Push an array to another array