Last characters of a string in Ruby

To get the last characters of a string in Ruby, use the following instructions. If the string is shorter than the selected number of characters, the entire string is returned.

a = '12345'
b = a[-3..-1] || a
puts b

Returns
345

Leave a Reply