Ruby on Rails, Io, Lisp, JavaScript, Dynamic Languages, Prototype-based programming and more...

Technoblog reader special: $10 off web hosting by FatCow!

Thursday, June 08, 2006

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.

module LastN
def last(n)

class String
include LastN

'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!


Anonymous josh said...

Um, the #last methods on Array and String already do that in Ruby.

10:41 PM, June 08, 2006

Blogger choonkeat said...

You mean Array and Range? String doesn't have #last and Range#last has no arguments, its actually Range#end

irb(main):006:0> [1,2,3,4,5].last(2)
=> [4, 5]
irb(main):007:0> "hello".last(2)
NoMethodError: undefined method `last' for "hello":String
from (irb):7
from :0
irb(main):008:0> (0..5).last(2)
ArgumentError: wrong number of arguments (1 for 0)
from (irb):8:in `last'
from (irb):8
from :0
irb(main):009:0> (0..5).last
=> 5

11:21 PM, June 08, 2006

Anonymous josh said...

D'oh! I was using the Rails console. ActiveSupport adds the String method for last(n). But Ruby 1.8.4 includes it for Array already.

10:12 AM, June 09, 2006

Anonymous Small Business Web Hosting said...

It's in ruby 1.8.3 aswell!

6:26 AM, October 01, 2006

Blogger V Jayant said...

You could also directly add the last(n) function to the String class

class String
def last(n)

"This works too".last(3) # => "too"

5:59 AM, April 13, 2010

Anonymous Leo said...

The ActiveSupport version also works when the length you want to clip to is greater than the length of the string, e.g.

"short string".last(20) -> "short string"

whereas your function will return nil

10:44 AM, June 06, 2011

Anonymous roger said...

yeah this returns nil if you give it too low a number. Which is really ruby's bug but hey...

4:24 AM, January 20, 2012

Blogger lina123 Chen said...

coach outlet store online
cheap oakley sunglasses
burberry sale
coach outlet
abercrombie fitch
ray ban uk
michael kors outlet online
kids lebron shoes
lv outlet
true religion sale
louis vuitton outlet
cheap ran bans
tory burch outlet
abercrombie and fitch
hollister clothing
ghd straighteners
air max 90
fendi handbags
michael kors outlet
jordan 11 columbia
louis vuitton outlet
louis vuitton outlet
cheap jerseys
gucci belt
coach factory outlet
louis vuitton outlet
ralph lauren uk
hermes birkin
michael kors outlet online
true religion sale
louis vuitton
tod's outlet
kobe bryant shoes
pandora bracelets
michael kors outlet
michael kors outlet online
jordan 4 toro
michael kors

6:19 PM, June 25, 2015

Blogger Nesma Ali said...

شركة تنظيف واجهات حجر بالرياض
كشف تسربات المياه
شركة تخزين اثاث بالرياض
شركة نقل اثاث بالرياض
شركة تنظيف فلل بالرياض
شركة تخزين عفش بالرياض
شركة نقل عفش بالرياض
شركة تنظيف موكيت بالرياض
شركة تنظيف مجالس بالرياض
شركة تنظيف شقق بالرياض
شركة تنظيف بيوت بالرياض
شركة تنظيف بيارات بالرياض
شركة رش مبيدات بالرياض
شركة مكافحة حشرات بالرياض
شركة عزل خزانات بالرياض

شركة تنظيف خزانات بالرياض
شركة تنظيف واجهات زجاج بالرياض

1:26 AM, September 02, 2015

Blogger Da Shuai Guo said...

oakley sunglasses, http://www.oakleysunglassescanada.com/
ray ban sunglasses outlet, http://www.raybansunglass.us.com/
cheap jordans, http://www.cheapjordanshoes.in.net/
coach outlet online, http://www.coachoutletstores.com.co/
mbt shoes outlet, http://www.mbtshoesoutlet.us.com/
michael kors factory outlet, http://www.michaelkorsfactoryoutlet.us.org/
coach outlet store, http://www.coachoutletonline-store.us.com/
asics, http://www.asicsisrael.com/
nike roshe run, http://www.rosherunshoessale.com/
iphone 6 cases, http://www.iphonecase.name/
ralph lauren uk, http://www.ralphlaurenoutletuk.org.uk/
real madrid jersey, http://www.realmadridjerseystore.com/
bears jerseys, http://www.chicagobearsjerseys.us/
ralph lauren outlet, http://www.ralphlaurenoutletuk.org.uk/
christian louboutin outlet, http://www.christianlouboutinoutlet.name/
polo ralph lauren, http://www.poloralphlauren.us.org/
louis vuitton outlet store, http://www.louisvuittonoutlet.in.net/
tory burch outlet, http://www.toryburch.in.net/
dolphins jerseys, http://www.miamidolphinsjersey.com/
replica handbags,replica handbags outlet,cheap handbags,purses and handbags,wholesale replica handbags,replica bags,bags outlet,replica handbags wholesale,cheap purses,discount handbags,handbags sale,wholesale purses,handbags and purses,replica purses
basketball shoes, http://www.basketballshoes.us.com/
coach outlet, http://www.coach-outlet-store.us.com/
true religion jeans, http://www.truereligionoutletstore.us.com/
ralph lauren outlet, http://www.ralphlaurenoutlet.in.net/
herve leger, http://www.herveleger.us.com/

1:07 AM, September 15, 2015


Post a Comment

Subscribe to Post Comments [Atom]

<< Home


If you like this blog, you might also like top photography schools.