Quantcast

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)
self[-n,n]
end
end

class String
include LastN
end

'This is a test'.last(4) # => 'test'

Enjoy!

You should follow me on twitter here.

Technoblog reader special: click here to get $10 off web hosting by FatCow!

12 Comments:

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 Zhenhong Bao said...

hermes outlet
ferragamo outlet
swarovski outlet
coach outlet online
toms shoes
ray-ban sunglasses
beats headphones
coach outlet online
mulberry handbags
chaussure louboutin
tory burch outlet online
cheap ray ban sunglasses
ray ban sunglasses
louis vuitton neverfull
michael kors wholesale
true religion outlet
mont blanc pens
cheap football shirts
kate spade handbags
mulberry bags
gucci outlet online
cheap jordans
nfl jersey wholesale
ralph lauren outlet
rolex watches for sale
tory burch outlet online
christian louboutin shoes
mulberry handbags
true religion jeans sale
true religion uk outlet
christian louboutin outlet
lebron james shoes
coach outlet
nike blazer pas cher
longchamp pliage
20160531zhenhong

6:17 PM, May 30, 2016

 
Blogger Zhenhong Bao said...

hermes outlet
ferragamo outlet
swarovski outlet
coach outlet online
toms shoes
ray-ban sunglasses
beats headphones
coach outlet online
mulberry handbags
chaussure louboutin
tory burch outlet online
cheap ray ban sunglasses
ray ban sunglasses
louis vuitton neverfull
michael kors wholesale
true religion outlet
mont blanc pens
cheap football shirts
kate spade handbags
mulberry bags
gucci outlet online
cheap jordans
nfl jersey wholesale
ralph lauren outlet
rolex watches for sale
tory burch outlet online
christian louboutin shoes
mulberry handbags
true religion jeans sale
true religion uk outlet
christian louboutin outlet
lebron james shoes
coach outlet
nike blazer pas cher
longchamp pliage
20160531zhenhong

6:18 PM, May 30, 2016

 
Blogger محمد الخطيب said...



شركة كشف تسربات المياه بالاحساء
كشف تسربات المياه بالاحساء
شركة عزل اسطح بالاحساء
شركة عزل خزانات المياه بالاحساء
شركه عزل مائى بالاحساء
شركة عزل حرارى بالاحساء
شركة ترميم منازل بالاحساء
شركة مكافحة حشرات بالاحساء
شركة رش مبيدات بالاحساء
شركة تسليك مجارى بالاحساء
شركة ترميم منازل بالاحساء

شركة كشف تسربات المياه بالجبيل
كشف تسربات المياه بالجبيل
شركة عزل اسطح بالجبيل
شركة عزل خزانات المياه بالجبيل
شركه عزل مائى بالجبيل
شركة عزل حرارى بالجبيل
شركة ترميم منازل بالجبيل
شركة مكافحة حشرات بالجبيل
شركة رش مبيدات بالجبيل
شركة تسليك مجارى بالجبيل


شركة كشف تسربات المياه بالخبر
كشف تسربات المياه بالخبر
شركة عزل اسطح بالخبر
شركة عزل خزانات المياه بالخبر
شركه عزل مائى بالخبر
شركة عزل حرارى بالخبر
شركة ترميم منازل بالخبر
شركة مكافحة حشرات بالخبر
شركة رش مبيدات بالخبر
شركة تسليك مجارى بالخبر

3:13 PM, July 10, 2016

 
Blogger Hua Cai said...

fitflops sale
ugg outlet
mulberry handbags sale
true religion outlet
michael kors outlet clearance
true religion jeans
new balance outlet
michael kors outlet clearance
ferragamo outlet
christian louboutin shoes
links of london
michael kors wholesale
tory burch outlet online
prada sneakers
true religion outlet
swarovski outlet
jordan pas cher
nike air force 1
calvin klein underwear
louis vuitton pas cher
true religion jeans
ferragamo outlet
ferragamo shoes sale
louis vuitton outlet store
tiffany and co
coach outlet store
gucci outlet
louis vuitton outlet
oakley sunglasses uk
true religion jeans
michael kors outlet online
mulberry outlet
nike free 5
celine outlet online
louis vuitton bags
20160804caihuali

10:43 PM, August 03, 2016

 
Blogger Fangyaya said...

cleveland cavaliers jerseys
canada goose
rolex watches outlet
nike factory outlet
ralph lauren uk
basketball shoes
tory burch outlet
polo ralph kids
coach factory outlet
ralph lauren polo
michael kors outlet clearance
coach factory online
uggs on sale
christian louboutin pas cher
nike air max 90
ray ban sunglasses outlet
true religion sale
toms shoes
authentic louis vuitton handbags
coach factory outlet online
washington wizards jerseys
ugg boots
michael kors purses
jordan 11
ugg boots
uggs outlet
kate spade outlet
gucci handbags
cartier watches
chenyingying20160908

7:13 PM, September 07, 2016

 
Blogger dong dong23 said...

nike nfl jerseys
mont blanc pens for sale
fitflops uk
oklahoma city thunder jerseys
coach outlet online
coach outlet
kate spade outlet
ugg boots
cartier watches
coach outlet store online clearances
michael kors handbags
tods outlet
polo ralph shirts
canada goose pas cher
lebron james shoes 12
canada goose coats
oakley store
kobe 9 shoes
coach factory outlet
fitflops
coach outlet
polo ralph lauren outlet
ugg store
cheap ray ban sunglasses
oakley sunglasses
nike factory outlet
ugg boots
ugg canada
ray ban sunglasses
mbt shoes
coach factory outlet
beats headphones
replica rolex watches
christian louboutin chaussures
coach outlet store online
gucci handbags
uggs for women
jordan retro 3
louis vuitton outlet
christian louboutin sale
20169.8wengdongdong

7:13 PM, September 07, 2016

 
Blogger chenlina said...

timberland boots
the north face jackets
christian louboutin paris
cheap nfl jerseys
kate spade
canada goose outlet
ray ban wayfarer
ugg boots paris
michael kors outlet
christian louboutin outlet
chenlina20161018

7:51 PM, October 17, 2016

 
Blogger Denature said...

Menghilangkan Kutil Pada KelaminJadi Apa Pilihan Pengobatan Yang Baik Untuk Kutil Kelamin Pria? Beberapa metode pengobatan yang dapat dicoba untuk menyembuhkan penyakit ini adalah melalui penggunaan krim dan salep. Selain itu bisa juga dengan resep dokter lain yang tujuanya untuk memusnahkan kutil tersebut. untuk cara pemesanana silahkan kunjungblog kami

obat herbal untuk wasir eksternalWasir tidak dapat menyebabkan kanker dubur. Dalam beberapa kasus yang jarang, orang dengan kanker dubur yang paling bertanggung jawab untuk memiliki wasir. Hal ini terjadi karena meningkatnya tekanan pada tumor.untuk cara pemesanana silahkan kunjungweb kami

obat kelamin pria keluar nanahCairan nanah pada pasien gonore tidak hanya muncul saat buang air kecil saja, tetapi juga dapat keluar melalui bintik-bintik merah yang muncul di kulit pasien. Perbedaan yang ketiga terletak pada ruam merah yang biasa muncul pada pasien sipilis. untuk cara pemesanana silahkan kunjungweb kami

obat ambeien yang berdarah

1:53 PM, November 16, 2016

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home

 

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