If you want to return only the first element of the array [{:s, 1, :xl, 4}], you can use the indexer operator [0] to access it. Here's an example:
main.rb60 chars4 lines
The output will be:
main.rb16 chars2 lines
By using arr[0]
, we access the first element of the array and assign it to the result
variable. Finally, we use puts result.inspect
to print the value of result
.
If you want to modify the original array and remove all other elements, you can use the slice!
method:
main.rb84 chars5 lines
This will output:
main.rb19 chars3 lines
The slice!
method removes and returns the element at index 0 from arr
, modifying it in place. The result
variable will hold the removed element, and arr
will be an empty array.
gistlibby LogSnag