SyntaxFix
Write A Post
Hire A Developer
Questions
Tags
Example:
a = [1, 3, 4, 5] b = [2, 3, 1, 5, 6]
How do I get the last value 5 in array a or last value 6 in array b without using a[3] and b[4]?
5
a
6
b
a[3]
b[4]
This question is related to ruby arrays
ruby
arrays
One other way, using the splat operator:
*a, last = [1, 3, 4, 5] STDOUT: a: [1, 3, 4] last: 5
Source: Stackoverflow.com