# Some small suggestions to Array

**URL:** <https://www.ruby-forum.com/t/some-small-suggestions-to-array/83352>\
**Category:** Ruby\
**Created:** [January 25, 2007, 2:40pm UTC](https://www.ruby-forum.com/t/some-small-suggestions-to-array/83352 "2007-01-25T14:40:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![SSSS\_SS\_SSSSS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c67d28/32.png) [@SSSS\_SS\_SSSSS\_S](https://www.ruby-forum.com/u/SSSS_SS_SSSSS_S)\
**Post date:** [January 25, 2007, 2:40pm UTC](https://www.ruby-forum.com/t/some-small-suggestions-to-array/83352/1 "2007-01-25T14:40:58Z")

</div>

I’m a new user of ruby from China. I love it very much. And I believe  
it will become better and better in future. During the study of ruby  
these days, I have some small suggestions to ruby.

The suggestions are about the array object, I think we may provide a  
much much more flexible way to refer the elements from an array.  
Suppose we have an array object [1,2,3,4,5], named “a”.  
1.Now we have: a[from, len], The first parameter means the result will  
start with a[from], and length is len. Then what about a[2,-3]? I think  
a negative second parameter should means an reversed array, that is,  
a[2,-3] shoule be [3,2,1].  
Additionally, we may introduce a more flexible way to express the  
“end with”. maybe like this: a[…2, 3] (means end with a[2], length is  
3, normal direction). here “…2” means end with a[2], and obviously we  
should let “2…” means “start with”.  
Some examples:  
a[2…, 3] == a[2,3] -\> [3,4,5]  
a[2…, -3] == a[2,-3] -\> [3,2,1]  
a[…2, 3] -\> [1,2,3]  
a[…2, 3] -\> [5,4,3]  
2.Now we have: a[2…3] and a[2…3] to discribe a closed interval and a  
semi-open interval. I think we should provide an easy way to discrible  
any kinds of interval, may be like this:  
2…4 means 2,3,4  
[2…4) means 2,3  
(2…4] means 3,4  
(2…4) means 3  
the way above may be not beautiful enough, but the function is  
required i guess.  
Moreover, I guess a[3…1] should means [4,3,2]. it is just the  
semantic of “from…to”  
3.Some much more ideas:  
a[i if odd?(i)] should be [2,4]  
a[[1,3,3,4]] should be [2,4,4,5]  
a[[1,3…4]] should be [2,4,5]  
a[:]{|a[i]|, odd?(a[i])} should be [1,3,5], the block works as a  
filter of the result.  
And what about the combination of all above?  
(Some ideas come from the grammar of matlab. Its array’s interface  
is great.)

---

<div class="post-metadata">

**Author:** ![SSSS\_SS\_SSSSS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c67d28/32.png) [@SSSS\_SS\_SSSSS\_S](https://www.ruby-forum.com/u/SSSS_SS_SSSSS_S)\
**Post date:** [January 26, 2007, 9:31am UTC](https://www.ruby-forum.com/t/some-small-suggestions-to-array/83352/2 "2007-01-26T09:31:23Z")

</div>

On Jan 25, 2007, at 16:40, çŽ‹è°¦ wrote:

> I’m a new user of ruby from China. I love it very much. And I believe  
> it will become better and better in future. During the study of ruby  
> these days, I have some small suggestions to ruby.

æ‚¨å¥½ï¼æˆ‘å¾ˆå–œæ¬¢ä»‹ç»æ‚¨ï¼  
I second your open ranges (a[2…] gives a subarray starting from the  
element with index 2 to the end) as i think they are quite intuitive.  
But I’m also a newcomer to Ruby, and I wonder whether there already are  
some beautiful ruby ways that give the desired results.

Your sincerely,  
Damian/Three-eyed Fish

---

<div class="post-metadata">

**Author:** ![SSSS\_SS\_SSSSS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c67d28/32.png) [@SSSS\_SS\_SSSSS\_S](https://www.ruby-forum.com/u/SSSS_SS_SSSSS_S)\
**Post date:** [January 26, 2007, 11:47am UTC](https://www.ruby-forum.com/t/some-small-suggestions-to-array/83352/3 "2007-01-26T11:47:45Z")

</div>

On 1/25/07, çŽ‹è°¦ [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m a new user of ruby from China. I love it very much. And I believe  
> it will become better and better in future. During the study of ruby  
> these days, I have some small suggestions to ruby.

Please, learn the language a bit more before you already give  
suggestions, i know this sounds not very nice and we really appreciate  
your suggestions, however, there are some quirks and standards in ruby  
that you may not be aware of as you write this.  
This should not mean that you have to be totally proficient in ruby  
after a couple of days, it only means that, before you critizise  
something you should have as much knowledge about it as possible.

> The suggestions are about the array object, I think we may provide a  
> much much more flexible way to refer the elements from an array.  
> Suppose we have an array object [1,2,3,4,5], named “a”.  
> 1.Now we have: a[from, len], The first parameter means the result will  
> start with a[from], and length is len. Then what about a[2,-3]? I think  
> a negative second parameter should means an reversed array, that is,  
> a[2,-3] shoule be [3,2,1].

negative indices count backwards, -1 is the equivalent of 0, but on  
the other side of the array.  
The way to do this right now:  
a.reverse[2,3]

# [3, 2, 1]

> Additionally, we may introduce a more flexible way to express the  
> “end with”. maybe like this: a[…2, 3] (means end with a[2], length is  
> 3, normal direction). here “…2” means end with a[2], and obviously we  
> should let “2…” means “start with”.  
> Some examples:  
> a[2…, 3] == a[2,3] → [3,4,5]  
> a[2…, -3] == a[2,-3] → [3,2,1]  
> a[…2, 3] → [1,2,3]  
> a[…2, 3] → [5,4,3]

This again is not possible, ruby ranges require a start and an end  
(and ruby has no infinite structures) Changing the behavior for Array  
only is not possible without changing Range, which would require an  
languagewide change for a construct that is quite hard to read. (keep  
in mind that String and tons of other classes share the behaviour of  
Array in this regard)

> 2.Now we have: a[2…3] and a[2…3] to discribe a closed interval and a  
> semi-open interval. I think we should provide an easy way to discrible  
> any kinds of interval, may be like this:  
> 2…4 means 2,3,4  
> [2…4) means 2,3  
> (2…4] means 3,4  
> (2…4) means 3  
> the way above may be not beautiful enough, but the function is  
> required i guess.

no, this again is a request to change Range in a _really_ non-ruby  
way. If you want a messy way to do such things, use [\*]

[\*(2…4)]

# [2, 3, 4]

[\*(2…4)]

# [2, 3]

> Moreover, I guess a[3…1] should means [4,3,2]. it is just the  
> semantic of “from…to”

Ranges build on the #succ method, the starting-element has to be \<=  
the end-element.

> 3.Some much more ideas:  
> a[i if odd?(i)] should be [2,4]  
> a[[1,3,3,4]] should be [2,4,4,5]  
> a[[1,3…4]] should be [2,4,5]  
> a[:]{|a[i]|, odd?(a[i])} should be [1,3,5], the block works as a  
> filter of the result.

class Symbol  
def to\_proc  
Proc.new { |\*args| args.shift. **send** (self, \*args) }  
end  
end

class Integer; def odd?; self % 2 != 0; end; end

a.select(&:odd?)

# [1, 3, 5]

a.reject(&:odd?)

# [2, 4]

I won’t comment on the other notations since i simply can’t grasp  
them. I am in no way an mathematician, but i am convinced that ruby  
should be very simple and easy to understand. Especially constructs  
that are used very often.

One could however propose to allow negative ranges, at least when the  
content responds to #pred… i think this has been brought forward  
quite often, but nobody has done an implementation of it so far.  
Symbol#to\_proc and Integer#odd? will be available with ruby1.9 as far  
as i am informed…

^ manveru

---

<div class="post-metadata">

**Author:** ![SSSS\_SS\_SSSSS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c67d28/32.png) [@SSSS\_SS\_SSSSS\_S](https://www.ruby-forum.com/u/SSSS_SS_SSSSS_S)\
**Post date:** [January 26, 2007, 10:17pm UTC](https://www.ruby-forum.com/t/some-small-suggestions-to-array/83352/4 "2007-01-26T22:17:11Z")

</div>

ÍõÇ« wrote:  
…

> a[[1,3…4]] should be [2,4,5]

The #values\_at method is useful for this:

irb(main):001:0\> a = (0…9).to\_a  
=\> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]  
irb(main):002:0\> a.values\_at(1…3, 5…7)  
=\> [1, 2, 3, 5, 6, 7]  
irb(main):003:0\> a.values\_at(1…3, 5, 7)  
=\> [1, 2, 3, 5, 7]

---

<div class="post-metadata">

**Author:** ![SSSS\_SS\_SSSSS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c67d28/32.png) [@SSSS\_SS\_SSSSS\_S](https://www.ruby-forum.com/u/SSSS_SS_SSSSS_S)\
**Post date:** [January 27, 2007, 10:30am UTC](https://www.ruby-forum.com/t/some-small-suggestions-to-array/83352/5 "2007-01-27T10:30:11Z")

</div>

On 1/26/07, Michael F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> One could however propose to allow negative ranges, at least when the content responds to #pred...

There is no #pred (yet?) ☹ [RCRchive](http://rcrchive.net/rcrs/6)

i think this has been brought forward

> quite often, but nobody has done an implementation of it so far.  
> Symbol#to\_proc and Integer#odd? will be available with ruby1.9 as far  
> as i am informed…
> 
> ^ manveru
> 
> > And what about the combination of all above?  
> > (Some ideas come from the grammar of matlab. Its array’s interface  
> > is great.)

Robert
