Last Updated: February 25, 2016
·
6.966K
· nicolaslazartekaqui

Ruby - Getting last character from a string

many ways

"abcd"[-1..-1]

=> "d"

or

"abcd"[-1,1]

=> "d"

or

"abcd".slice(-1,1)

=> "d"

or

"abcd".slice(-1).chr

=> "d"

or

"abcd"[ /.$/ ]

=> "d"

or

"abcd".reverse[0,1]

=> "d"

or

"abcd".split('').pop

=> "d"

or

class String
  def last
    self[-1,1]
  end
end

"abc".last

=> "d"