[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

some small suggestions to Array

??

1/25/2007 1:39:00 PM

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.)

3 Answers

Damian Terentyev

1/26/2007 8:31:00 AM

0

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

Joel VanderWerf

1/26/2007 9:16:00 PM

0

í??? 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]

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Seon

4/22/2010 12:24:00 AM

0



"zeez" <blinkingblythe01@gmail.com> wrote in message
news:09cdc4ac-0df4-4bf8-8fcf-9d212c59452d@w39g2000yqb.googlegroups.com...
> Hmmm...Might have been because Bu$h was infact acting like a Nazi and
> doing things that were very anti freedom, detrimental to human rights
> and life.

But people didn't call Bush a Nazi because of racial reasons. Just like the
far right loons don't call Obama a Nazi because of racial reasons. They
think health care is socialism no matter what the color of the presidents
skin is.