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"
Written by Nícolas Lazarte Kaqui
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Related Tags
#ruby
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#