String and Array Slices
Recently began to study Ruby and lacks some things from the python. I wish they were added to Ruby.
"hello, world!"[3:9:2] # 'l,w' "hello, world!"[3:9] # 'lo, wo' [1,2,3,4,5][1:3] # [2, 3] [1,2,3,4,5][1:5:2] # [2, 4]
Updated by zverok (Victor Shepelev) about 3 years ago
We do have ranges to slice:
"hello, world!"[3...9] # => "lo, wo" "hello, world!"[3..9] # two dots mean including end # => "lo, wor" [1,2,3,4,5][1...3] # => [2, 3] [1,2,3,4,5][-3..] # => [3, 4, 5] [1,2,3,4,5][-3...-1]
Though, Ruby's slicing kinda less powerful than Python's: currently, we don't have a way to express the third argument (step), though potential syntax (in the form of
(3...9) % 2) was introduced in the latest version.
I believe this last feature was not really popular requirement to add, as lacking really compelling real-life use cases.
Updated by shyouhei (Shyouhei Urabe) about 3 years ago
- Status changed from Open to Feedback
As @zverok (Victor Shepelev) mentioned there are String / Array slicing methods. Not a 1-to-1 translation of Python though. Is it enough for you? If not, tell us in detail what you want.
Updated by shevegen (Robert A. Heiler) about 3 years ago
I use both ruby and python; ruby significantly more than python though.
First, to the direct comparison - I am against using : in ruby. It does
not make a whole lot of sense (to me).
For equivalent purposes we have:
"hello, world!"[3:9] # => 'lo, wo'
"hello, world!"[3,6] # => "lo, wo"
I think it would confuse people if ruby were to add a notation using :,
in particular if we'd also use the same way as python counts here (e. g.
3:9 rather than 3,6) .
As for the step parameter, I have no strong feelings pro or con, although I
personally never needed it. I am not sure if it is really needed either.
Programming languages are different and while some features in other
languages may appear useful, I am not sure that a 1:1 translation really
often makes sense. In this case here, I think retaining the old way would
be best, to avoid adding potential confusion, in particular when the : variants
would behave like python does (and if they would not, then you could suddenly
claim that this is confusing to python-users, so this would only add further
I believe this last feature was not really popular requirement
to add, as lacking really compelling real-life use cases.
Agreed. This may also be a question whether people would really need a
step parameter to begin with.
On a side note - I disagree that ruby "lacks" something or has a "less
powerful" anything here. ;)
Anyway, this is just my opinion - you only have to convince matz about
pros/cons of a feature in the end.
By the way, to reverse an array, this is more rubyish:
[1,2,3,4,5].reverse => [5, 4, 3, 2, 1]
I do not think [::-1] is elegant or makes sense from the context of ruby.
Python has another approach to functions as such - they seem to have a
much higher imminent focus in python. I have no real problem with python,
but I like ruby's approach and philosophy more in this regard, since it
seems to make more sense to me.