Project

General

Profile

Feature #16017

String and Array Slices

Added by D1mon (Dim F) about 2 months ago. Updated about 2 months ago.

Status:
Feedback
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:93893]

Description

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]

History

Updated by D1mon (Dim F) about 2 months ago

[1,2,3,4,5][-3:]
# [3, 4, 5]
[1,2,3,4,5][-3:-1]
# [3, 4]
[1,2,3,4,5][::-1]
# [5, 4, 3, 2, 1]
[1,2,3,4,5][::2]
# [1, 3, 5]

Updated by zverok (Victor Shepelev) about 2 months ago

I recommend String and Array docs, they are pretty straightforward and list everything you might do with them.

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 2 months 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 D1mon (Dim F) about 2 months ago

I tried Python syntax and did not think it would be different. Well, if it's not difficult for you, then you can add the familiar syntax [::] Thank.

Updated by shevegen (Robert A. Heiler) about 2 months 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:

Python:

"hello, world!"[3:9] # => 'lo, wo'

Ruby:

"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
different expectations).

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.

Also available in: Atom PDF