array element access

T

Tomas Fischer

Hi,

I've got an array a= [1,2,3,4,5,6] and want to access each second
element:
b=[2,4,6]. How is this done in ruby?

I know, that I can use a for-loob and modulo operator, but I think there
is a "ruby way" of doing this.

Thanks.
tomas
 
M

Mike Austin

Tomas said:
Hi,

I've got an array a= [1,2,3,4,5,6] and want to access each second
element:
b=[2,4,6]. How is this done in ruby?

I know, that I can use a for-loob and modulo operator, but I think there
is a "ruby way" of doing this.

Thanks.
tomas

I've always been intrigued by creating a generalized enumeration system. Have
you guys seen this: http://www.rubyweeklynews.org/20050710 : accessing index
inside map. For example:

(1..10).collect.with_index { |v, i| v * i }

I think that is sweet! I wonder if it can be generalized enough to do this:

file.collect.each_byte.with_index { |b, i| b + 1 }


Mike
 
D

dblack

Hi --

Tomas said:
Hi,

I've got an array a= [1,2,3,4,5,6] and want to access each second element:
b=[2,4,6]. How is this done in ruby?

I know, that I can use a for-loob and modulo operator, but I think there is
a "ruby way" of doing this.

Thanks.
tomas

I've always been intrigued by creating a generalized enumeration system.
Have you guys seen this: http://www.rubyweeklynews.org/20050710 : accessing
index inside map. For example:

(1..10).collect.with_index { |v, i| v * i }

I think that is sweet! I wonder if it can be generalized enough to do this:

file.collect.each_byte.with_index { |b, i| b + 1 }

I'm not sure why, but to my eye this has always looked somewhat
obscure. I guess it's something about having to read to the right,
even to the extent of not seeing a block, before I know what's being
returned (array vs. enumerator). It's almost as if the call to
collect wasn't a "real" call to collect, but rather a kind of
place-holder for a collect operation. I'm not sure.


David

--
David A. Black (dblack@wobblini.net)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" chapters now available
from Manning Early Access Program! http://www.manning.com/books/black
 
M

Mike Austin

Hi --

Tomas said:
Hi,

I've got an array a= [1,2,3,4,5,6] and want to access each second
element:
b=[2,4,6]. How is this done in ruby?

I know, that I can use a for-loob and modulo operator, but I think
there is a "ruby way" of doing this.

Thanks.
tomas

I've always been intrigued by creating a generalized enumeration
system. Have you guys seen this:
http://www.rubyweeklynews.org/20050710 : accessing index inside map.
For example:

(1..10).collect.with_index { |v, i| v * i }

I think that is sweet! I wonder if it can be generalized enough to do
this:

file.collect.each_byte.with_index { |b, i| b + 1 }

I'm not sure why, but to my eye this has always looked somewhat
obscure. I guess it's something about having to read to the right,
even to the extent of not seeing a block, before I know what's being
returned (array vs. enumerator). It's almost as if the call to
collect wasn't a "real" call to collect, but rather a kind of
place-holder for a collect operation. I'm not sure.

Ok, what if you rearrange the order of the calls:

file.each_byte.with_index.collect { |b, i| b + 1 }

That's more left-to-right object-oriented sounding. Come to think of it I like
this better because now collect is at the end, and that is the the final intent
(with the block being next to it).

Mike
 
D

dblack

Hi --

Hi --

Tomas Fischer wrote:
Hi,

I've got an array a= [1,2,3,4,5,6] and want to access each second
element:
b=[2,4,6]. How is this done in ruby?

I know, that I can use a for-loob and modulo operator, but I think there
is a "ruby way" of doing this.

Thanks.
tomas

I've always been intrigued by creating a generalized enumeration system.
Have you guys seen this: http://www.rubyweeklynews.org/20050710 :
accessing index inside map. For example:

(1..10).collect.with_index { |v, i| v * i }

I think that is sweet! I wonder if it can be generalized enough to do
this:

file.collect.each_byte.with_index { |b, i| b + 1 }

I'm not sure why, but to my eye this has always looked somewhat
obscure. I guess it's something about having to read to the right,
even to the extent of not seeing a block, before I know what's being
returned (array vs. enumerator). It's almost as if the call to
collect wasn't a "real" call to collect, but rather a kind of
place-holder for a collect operation. I'm not sure.

Ok, what if you rearrange the order of the calls:

file.each_byte.with_index.collect { |b, i| b + 1 }

That's more left-to-right object-oriented sounding. Come to think of it I
like this better because now collect is at the end, and that is the the final
intent (with the block being next to it).

There's still something I don't like about using method-chaining
syntax for this. It may just be a failure of imagination on my part.


David

--
David A. Black (dblack@wobblini.net)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" chapters now available
from Manning Early Access Program! http://www.manning.com/books/black
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,297
Messages
2,571,525
Members
48,248
Latest member
LouveniaUc

Latest Threads

Top