Picking arbitrary elements from an array

Anyone like the idea of being able to pick arbitrary elements from
an array?

foo = [ 0, 2, 4, 6, 8, 10, 12, 14 ]
p foo[0;6;7] # => returns [ 0, 12, 14 ]

Or is there a way to do this now I’m unaware of ?

···

Jim Hranicky, Senior SysAdmin UF/CISE Department |
E314D CSE Building Phone (352) 392-1499 |
jfh@cise.ufl.edu http://www.cise.ufl.edu/~jfh |


“Given a choice between a complex, difficult-to-understand, disconcerting
explanation and a simplistic, comforting one, many prefer simplistic
comfort if it’s remotely plausible, especially if it involves blaming
someone else for their problems.”
– Bob Lewis, Infoworld

  foo = [ 0, 2, 4, 6, 8, 10, 12, 14 ]
  p foo[0;6;7] # => returns [ 0, 12, 14 ]

Array#indices (deprecated in 1.7.*)

pigeon% ruby -e 'a =[ 0, 2, 4, 6, 8, 10, 12, 14 ]; p a.indices(0, 6, 7)'
[0, 12, 14]
pigeon%

pigeon% ./ruby -ve 'a =[ 0, 2, 4, 6, 8, 10, 12, 14 ]; p a.indices(0, 6, 7)'
ruby 1.7.3 (2002-09-13) [i686-linux]
-e:1: warning: Array#indices is deprecated; use Array#select
[0, 12, 14]
pigeon%

Guy Decoux

Gak – I read right over that about 5 times…thanks,

Jim

···

On Wed, 18 Sep 2002 23:04:02 +0900 ts decoux@moulon.inra.fr wrote:

foo = [ 0, 2, 4, 6, 8, 10, 12, 14 ]
p foo[0;6;7] # => returns [ 0, 12, 14 ]

Array#indices (deprecated in 1.7.*)

pigeon% ruby -e ‘a =[ 0, 2, 4, 6, 8, 10, 12, 14 ]; p a.indices(0, 6, 7)’
[0, 12, 14]
pigeon%

pigeon% ./ruby -ve ‘a =[ 0, 2, 4, 6, 8, 10, 12, 14 ]; p a.indices(0, 6, 7)’
ruby 1.7.3 (2002-09-13) [i686-linux]
-e:1: warning: Array#indices is deprecated; use Array#select
[0, 12, 14]

Is there going to be a 1.7+ replacement?

···

— “James F.Hranicky” jfh@cise.ufl.edu wrote:

On Wed, 18 Sep 2002 23:04:02 +0900 > ts decoux@moulon.inra.fr wrote:

foo = [ 0, 2, 4, 6, 8, 10, 12, 14 ]
p foo[0;6;7] # => returns [ 0, 12, 14 ]

Array#indices (deprecated in 1.7.*)

=====

Use your computer to help find a cure for cancer: http://members.ud.com/projects/cancer/

Yahoo IM: michael_s_campbell


Do you Yahoo!?
Yahoo! News - Today’s headlines

> Array#indices (deprecated in 1.7.*)

Is there going to be a 1.7+ replacement?

Yes, Array#select

pigeon% ./ruby -ve 'p [0, 1, 2].indices(0, 2)'
ruby 1.7.3 (2002-09-13) [i686-linux]
-e:1: warning: Array#indices is deprecated; use Array#select
[0, 2]
pigeon%

pigeon% ./ruby -ve 'p [0, 1, 2].select(0, 2)'
ruby 1.7.3 (2002-09-13) [i686-linux]
[0, 2]
pigeon%

Guy Decoux

Is there going to be a 1.7+ replacement?

Yes, Array#select

pigeon% ./ruby -ve ‘p [0, 1, 2].indices(0, 2)’
ruby 1.7.3 (2002-09-13) [i686-linux]
-e:1: warning: Array#indices is deprecated; use Array#select

<D’oh>, I missed that part from your original post; thanks.

···

=====

Use your computer to help find a cure for cancer: http://members.ud.com/projects/cancer/

Yahoo IM: michael_s_campbell


Do you Yahoo!?
Yahoo! News - Today’s headlines

How does this relate to Enumerable#select,
which Array mixes in? Do we somehow get
the functionality of both?

Hal

···

----- Original Message -----
From: “ts” decoux@moulon.inra.fr
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Cc: ruby-talk@ruby-lang.org
Sent: Wednesday, September 18, 2002 10:31 AM
Subject: Re: Picking arbitrary elements from an array

Array#indices (deprecated in 1.7.*)

Is there going to be a 1.7+ replacement?

Yes, Array#select

Hi –

From: “ts” decoux@moulon.inra.fr
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Cc: ruby-talk@ruby-lang.org
Sent: Wednesday, September 18, 2002 10:31 AM
Subject: Re: Picking arbitrary elements from an array

Array#indices (deprecated in 1.7.*)

Is there going to be a 1.7+ replacement?

Yes, Array#select

How does this relate to Enumerable#select,
which Array mixes in? Do we somehow get
the functionality of both?

Judging from some 1.7.3 experimenting, it seems that Array#select, if
given an argument, acts like #indices, and if given a block but no
argument, acts like #find_all (for which it used to be a synonym). If
given both, it raises an exception.

I don’t know if this is still under discussion, but in case it is…
This new flavor of #select doesn’t feel to me like it has the right
name. Maybe I’m just used to the idea of select as involving a test,
rather than an index – and in fact it still does, some of the time.
It’s really the same name for two very different things.

My instincts, such as they are, would lead me to lean toward a
multi-arg version of #at:

[1,2,3].at(1,2) => [2,3]

Or is there (not for the first time) a subtlety or two I’m not
getting?

David

···

On Thu, 19 Sep 2002, Hal E. Fulton wrote:

----- Original Message -----


David Alan Black | Register for RubyConf 2002!
home: dblack@candle.superlink.net | November 1-3
work: blackdav@shu.edu | Seattle, WA, USA
Web: http://pirate.shu.edu/~blackdav | http://www.rubyconf.com

[1,3,6].at(1,2) => [3,6] PROPOSED
[1,3,6].at(1) => 3 ACTUAL
[1,3,6].at(1) => [3] CONSISTENT WITH PROPOSAL

#indices always returns an array, and has a really nice name, to boot. I’m
sorry to see it go.

BTW it’s very important to consistently return an array, in case you call it in
the following manner:

indices = some_array
values = arr.indices(*indices)

The subsequent code expects “values” to be an array, even if “indices” contains
zero or one elements.

I agree that overloading “select” for two different purposes is a bad idea.

Gavin

···

----- Original Message -----
From: dblack@candle.superlink.net

How does this relate to Enumerable#select,
which Array mixes in? Do we somehow get
the functionality of both?

Judging from some 1.7.3 experimenting, it seems that Array#select, if
given an argument, acts like #indices, and if given a block but no
argument, acts like #find_all (for which it used to be a synonym). If
given both, it raises an exception.

I don’t know if this is still under discussion, but in case it is…
This new flavor of #select doesn’t feel to me like it has the right
name. Maybe I’m just used to the idea of select as involving a test,
rather than an index – and in fact it still does, some of the time.
It’s really the same name for two very different things.

My instincts, such as they are, would lead me to lean toward a
multi-arg version of #at:

[1,2,3].at(1,2) => [2,3]

Or is there (not for the first time) a subtlety or two I’m not
getting?

David