a[2,a.size] works, but I find it ugly because the given length is larger
than the real length of the result.
a[2,a.size - 2] is okay, but it is not DRY.
Is there any elegant way, like a[2:] in Python?
(Well, I know that I can write my own [] method for String class.)
a[2,a.size] works, but I find it ugly because the given length is larger
than the real length of the result.
a[2,a.size - 2] is okay, but it is not DRY.
Thank you, I`ve only tried a[2,-1] before the letter. My fault.
By the way, ranges ending with lesser number than beginning seems nasty
objects to me. Their “member?” and “each” methods are not usable. Are
they good for anything beyond string slicing?
Thank you, I`ve only tried a[2,-1] before the letter. My fault.
By the way, ranges ending with lesser number than beginning seems nasty
objects to me. Their “member?” and “each” methods are not usable. Are they
good for anything beyond string slicing?
They’re not good for that either; [2,-1] isn’t a range object
I can’t remember exactly what’s been said in the past about backwards
ranges, but I know stuff has been said. There’s a ton of range
discussion in the ruby-talk archives. You’ll find just about every
possible like, dislike, suggestion for change, etc., represented.
It also doesn’t work, at least not the way the OP wanted it to. But
2…-1 does.
% ruby -v
ruby 1.8.4 (2005-12-24) [powerpc-darwin8.4.0]
% irb
irb(main):001:0> “hello world”[2, -1]
=> nil
irb(main):002:0> “hello world”[2…-1]
=> “llo world”
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.