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!

32 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

 
Anonymous Anonymous 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

 
Anonymous Anonymous 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

 
Blogger chenmeinv0 said...

louboutin femme
ed hardy outlet
cheap ugg boots
the north face
hilfiger jeans
retro jordan shoes
cheap ray ban sunglasses
gucci outlet online
coach factory outlet
air jordan shoes
2017.1.4xukaimin

10:28 PM, January 03, 2017

 
Blogger dada xu said...

nike blazer low
christian louboutin shoes
ralph lauren uk
ed hardy sale
cheap jordans
chicago bulls jerseys
michael kors
yeezy boost
adidas superstar shoes
michael kors outlet
chenshanshan20170315

1:19 AM, March 15, 2017

 
Blogger Unknown said...

buffalo bills jerseys
packers jerseys
hugo boss sale
falcons jersey
nike air force 1
michael kors outlet online
red valentino
rolex replica watches
toms shoes
oakley sunglasses

12:26 AM, March 16, 2017

 
Blogger shengda xu said...

michael kors bags
christian louboutin outlet
ralph lauren sale
ray ban sale
coach factory outlet online
yeezy boost 350
coach outlet online
moncler jackets
parada bags
cheap jerseys
xushengda0418

6:49 AM, April 18, 2017

 
Blogger shengda xu said...

discount oakley sunglasses
hollister kids
vans store
polo ralph lauren outlet online
adidas outlet store
jerseys cheap
adidas outlet online
vans outlet
yeezy boost 350
moncler jackets
xushengda0714

2:45 AM, July 14, 2017

 
Blogger Quang Đào Duy said...

Chung cư Green Pearl nằm trong quần thể Dự án Green Pearl tại số 378 Minh Khai, Vĩnh Tuy, Hai Bà Trưng, Hà Nội là một vị trí đắc địa thuộc cửa ngõ phía Nam của Thủ đô, tiếp giáp với đường Minh và KĐT times city. Căn hộ chung cư cao cấp Green Pearl sẽ mở ra không gian sống xanh, trong lành, KHÔNG ồn ào, KHÔNG khói bụi ngay trong lòng đô thị.
Chung cư Green Pearl

7:21 AM, August 27, 2017

 
Blogger dong dong23 said...

ralph lauren outlet
salomon boots
moncler outlet
burberry
rolex watches
louboutin
replica watches
roshe run
ghd hair straighteners
canada goose pas cher
2017.11.20chenlixiang

2:54 AM, November 20, 2017

 
Blogger Alyson ning said...

I don't apperceive what it is about the bendable blow and matte finish, but I accept a anemic atom for printed suede Christisn Louboutin Outlet. One of the best brands to cull this off is YSL Wallet Outlet. I accept to acquaint you about a backpack attraction of mine. I am in fact mad at myself that I did not splurge on the Ralph Lauren Polo Outlet Croc Muse Two which has two colors that I admire with a sueced croc pattern. To see a formed suede accomplishment off a Red Bottoms Outlet that I already adore, my affection melts. Replica Watches are additionally pretty reflective of their user's persona. Amazing furthermore multi-colored Replica Watches are commonly worn because of the younger. Conversely, exquisite dress Swiss Rolex Replica are regularly sported by highly developed gentlemen. And then usually there's the luxurious Swiss Replica Watches worn by the individuals who're properly to complete. Check out observing your mates furthermore you can before long see how the layouts are actually ranging.

2:01 AM, April 26, 2018

 
Blogger jing dong said...

yeezy shoes
ultra boost
nike air force
jordan 4
hermes belt
van cleef
michael kors outlet online
true religion jeans
yeezys
yeezy

1:00 AM, May 26, 2018

 
Anonymous jasa seo murah beragaransi said...

Back again for read your fresh content in this blog
Jasa seo murah bergaransi

Distributor Furniture jepara

Distributor Furniture Rotan

Jasa ART Bandung

yayasan Pembantu Bandung

Jasa baby sitter bandung

Yayasan baby sitter bandung

Situs Berita terkini

Detik Siang

10:21 AM, June 03, 2018

 
Anonymous AGUNGQQ Agen DominoQQ, BandarQ, Capsa Susun, QQ Online Terpercaya di Indonesia said...

good job and very briliant for write this topic
QQliga.com Situs Bandar Taruhan Agen Judi Bola Terbesar Indonesia

AGUNGQQ Agen DominoQQ, BandarQ, Capsa Susun, QQ Online Terpercaya di Indonesia

Agen Bandar66 Online Judi Sakong Capsa BandarQ Domino99 Keris99

ituCasino Agen Bola Sbobet Piala Dunia 2018 Live Casino Online

Judi Sbobet LiveCasino 338a Agen Bola Piala Dunia Online KiosCasino

10:21 AM, June 03, 2018

 
Anonymous cara menggugurkan kandungan said...

allways good idea for write high quality content in this blog
Cara Menggugurkan kandungan

Obat Aborsi

obat aborsi

cara menggugurkan kandungan

obat aborsi

cara menggugurkan kandungan

cara menggugurkan kandungan

obat aborsi

obat penggugur kandungan

10:23 AM, June 03, 2018

 
Anonymous agen domino qq terpercaya said...

nice post and i like to read your unique content
agen dominoqq terpercaya

BwinQQ.casino agen dominoqq terpercaya

BwinQQ.casino situs dominoqq terpercaya

BwinQQ.casino situs poker online terpercaya

BwinQQ.casino agen poker terpercaya

bandarqiu99.info situs domino99 terpercaya

Mrjudibola.com agen bola terpercaya

Mrjudibola.com Bandar bola terpercaya

Daftar Poker Online

Poker Online Indonesia

10:24 AM, June 03, 2018

 
Blogger Stjsrty Xtjsrty said...

zzzzz2018.7.16
canada goose outlet
moncler jackets
louboutin shoes
canada goose jackets
dsquared
mbt shoes
moncler uk
true religion outlet
michael kors outlet online
basketball shoes

7:10 PM, July 15, 2018

 
Anonymous vibrator sex wanita said...

thanks for sharing, nice post.


Vibrator Pretty Love Snappy

Vibrator Sensual Massager

Teaser Tongue Vibrator Tangan

Vibrator Climax Combo Massager

Vibrator Cute Dragon

Vibrator Fantasy Bliss

Vibrator Egg Capsul Wireless

Vibrator Fairy Mic

Vibrator Finger Ring Dua Jari

Vibrator Kondom Beauty Kit

11:02 AM, August 10, 2018

 
Blogger 5689 said...

zzzzz2018.9.2
christian louboutin sale
off white shoes
pandora outlet
ugg boots clearance
canada goose outlet
canada goose jackets
moncler outlet
christian louboutin outlet
soccer boots
christian louboutin shoes

6:22 PM, September 01, 2018

 
Blogger 洛噷 said...

2018906 leilei3915
ugg boots outlet
louboutin shoes
coach outlet
polo ralph lauren
michael kors outlet online
pandora jewelry
coach factory outlet
ray ban sunglasses
fitflops sale
uggs outlet

12:02 AM, September 06, 2018

 
Anonymous Cara Mengoabti Bisul said...

This article is interesting and useful. Thank you for sharing. And let me share an article about health that God willing will be very useful. Thank you :)

Cara Mengobati radang Usus
Cara Mengatasi sakit pundak dan Leher kaku
Obat Penambah Berat Badan Alami
Cara Mengatasi Sakit Perut saat Haid
Obat Tradisional Radang Tenggorokan
solusi masalah kewanitaan

7:05 PM, September 20, 2018

 
Blogger chenlili said...

moncler jackets
oakley sunglasses
louboutin outlet
coach outlet
red bottoms
adidas nmd shoes
ray ban sunglasses
ugg outlet
nike kyrie 3
pandora jewelry
yaoxuemei20181022

6:18 PM, October 21, 2018

 
Blogger obat telat bulan said...

thanks for sharing 082210743003


obat aborsi

obat telat bulan

obat penggugur

jual obat aborsi

obat aborsi termurah


obat penggugurkandungan

jual obat penggugur kandungan

jual obat aborsi termanjur

obat penggugur termurah


obat telat bulan

jual obat telat bulan

klinik obat telat bulan

obat telat bulan termanjur

11:18 PM, December 05, 2018

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home

 

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