Last n Characters of a Ruby String
Almost without fail people come to me after using Ruby for a while and ask if there is any easy way to get the last n characters of a string. The easiest way to do it without extending classes is:
str = 'This is a test'
srt[-4,4] # => 'test'
However if you are willing to mixin a nice little method you will forevermore have rapid access to the last characters of a string.
'This is a test'.last(4) # => 'test'
You should follow me on twitter here.
Technoblog reader special: click here to get $10 off web hosting by FatCow!