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!

8 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

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home

 

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