remove an element from the end of an array in ruby

To remove an element from the end of an array in Ruby, you can use the pop method. This method removes the last element of the array and returns it.

main.rb
my_array = [1, 2, 3, 4, 5]
removed_element = my_array.pop
puts "Removed element: #{removed_element}"
puts "Array after removing element: #{my_array}"
150 chars
5 lines

This will output:

main.rb
Removed element: 5
Array after removing element: [1, 2, 3, 4]
62 chars
3 lines

related categories

gistlibby LogSnag