WoNáDo
2/20/2007 1:34:00 PM
Josselin schrieb:
>
> if i have
>
> my_list = "2-131-25-5558-247-68"
>
> and I want to get rid of the last element...
> I wrote :
>
> my_list.split('-') - my_list.split('-').last.to_a =>
> "22-13-25-58-47"
>
> which runs well.. but isn't too complex.... ?
>
> tfyl
>
> joss
>
irb(main):001:0> my_list = "2-131-25-5558-247-68"
=> "2-131-25-5558-247-68"
irb(main):002:0> my_list.split('-') - my_list.split('-').last.to_a
=> ["2", "131", "25", "5558", "247"]
irb(main):003:0> my_list.split('-')[0..-1].join('-')
=> "2-131-25-5558-247-68"
irb(main):004:0> my_list.split('-')[0..-2].join('-')
=> "2-131-25-5558-247"
irb(main):005:0> my_list.split('-')[0..-3].join('-')
=> "2-131-25-5558"
irb(main):006:0> my_list.sub(/(?:-\d+)$/, '')
=> "2-131-25-5558-247"
irb(main):007:0> my_list.sub(/(?:-\d+){2}$/, '')
=> "2-131-25-5558"
irb(main):008:0> my_list.sub(/(?:-\d+){3}$/, '')
=> "2-131-25"
Some ideas...
Wolfgang Nádasi-Donner