06 Nov 2021   ruby


You can access the last n characters of a string by using and endless range:

credit_card = "1234•5678•7126•5555"
last_four = (-4..)
puts "Last four: #{credit_card[last_four]}"
=> Last four: 5555