# Array element access

**URL:** <https://rubytalk.org/t/array-element-access/25698>\
**Category:** ruby-talk\
**Created:** [12 March 2006 20:34 UTC](https://rubytalk.org/t/array-element-access/25698 "2006-03-12T20:34:40Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tomas\_Fischer2](https://avatars.discourse-cdn.com/v4/letter/t/d78d45/32.png) [@Tomas\_Fischer2](https://rubytalk.org/u/Tomas_Fischer2)\
**Post date:** [12 March 2006 20:34 UTC](https://rubytalk.org/t/array-element-access/25698/1 "2006-03-12T20:34:40Z")

</div>

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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Daniel\_Harple](https://avatars.discourse-cdn.com/v4/letter/d/5daacb/32.png) [@Daniel\_Harple](https://rubytalk.org/u/Daniel_Harple)\
**Post date:** [12 March 2006 20:54 UTC](https://rubytalk.org/t/array-element-access/25698/2 "2006-03-12T20:54:04Z")

</div>

require 'enumerator'  
a = (1..10).to\_a  
p a.enum\_for(:each\_slice, 2).collect { |m| m[1] } # -\> [2, 4, 6, 8, 10]

-- Daniel

> **···**
>
> On Mar 12, 2006, at 9:34 PM, 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
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Harold\_Hausman](https://avatars.discourse-cdn.com/v4/letter/h/ac91a4/32.png) [@Harold\_Hausman](https://rubytalk.org/u/Harold_Hausman)\
**Post date:** [12 March 2006 21:05 UTC](https://rubytalk.org/t/array-element-access/25698/3 "2006-03-12T21:05:02Z")

</div>

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

# This is ugly, but it works:

a = [1,2,3,4,5,6]  
(1..a.length).step(2) { |i| puts a[i] }

# -Harold

---

<div class="post-metadata">

**Author:** ![Farrel\_Lifson](https://avatars.discourse-cdn.com/v4/letter/f/3da27b/32.png) [@Farrel\_Lifson](https://rubytalk.org/u/Farrel_Lifson)\
**Post date:** [12 March 2006 21:45 UTC](https://rubytalk.org/t/array-element-access/25698/4 "2006-03-12T21:45:45Z")

</div>

array.inject(false) do |alternate,element|  
&nbsp;&nbsp;if alternate  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Your processing  
&nbsp;&nbsp;end  
&nbsp;&nbsp;!alternate  
end

> **···**
>
> On 3/12/06, Tomas Fischer \<tomas\_fischer99\_spamremoveit\_@yahoo.com\> 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
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Luc\_Heinrich](https://avatars.discourse-cdn.com/v4/letter/l/958977/32.png) [@Luc\_Heinrich](https://rubytalk.org/u/Luc_Heinrich)\
**Post date:** [12 March 2006 23:32 UTC](https://rubytalk.org/t/array-element-access/25698/5 "2006-03-12T23:32:04Z")

</div>

irb(main):001:0\> a = [1,2,3,4,5,6]  
=\> [1, 2, 3, 4, 5, 6]  
irb(main):002:0\> a.select {|e| a.index(e) % 2 == 1 }  
=\> [2, 4, 6]

> **···**
>
> On 12 mars 06, at 21:34, Tomas Fischer wrote:
> 
> > 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?
> 
> --  
> Luc Heinrich - luc@honk-honk.com - [http://www.honk-honk.com](http://www.honk-honk.com)

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [12 March 2006 23:50 UTC](https://rubytalk.org/t/array-element-access/25698/6 "2006-03-12T23:50:45Z")

</div>

Fixnum#step is one of the easiest ways to do this:

&nbsp;&nbsp;&nbsp;harp:~ \> cat a.rb  
&nbsp;&nbsp;&nbsp;a = [1, 2, 3, 4, 5, 6]  
&nbsp;&nbsp;&nbsp;1.step(a.size, 2){|i| p a[i]}

&nbsp;&nbsp;&nbsp;harp:~ \> ruby a.rb  
&nbsp;&nbsp;&nbsp;2  
&nbsp;&nbsp;&nbsp;4  
&nbsp;&nbsp;&nbsp;6

regards.

-a

> **···**
>
> On Mon, 13 Mar 2006, 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
> 
> --  
> share your knowledge. it's a way to achieve immortality.  
> - h.h. the 14th dali lama

---

<div class="post-metadata">

**Author:** ![Mike\_Austin4](https://avatars.discourse-cdn.com/v4/letter/m/fbc32d/32.png) [@Mike\_Austin4](https://rubytalk.org/u/Mike_Austin4)\
**Post date:** [13 March 2006 23:23 UTC](https://rubytalk.org/t/array-element-access/25698/7 "2006-03-13T23:23:45Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![Tim\_Hunter](https://avatars.discourse-cdn.com/v4/letter/t/7feea3/32.png) [@Tim\_Hunter](https://rubytalk.org/u/Tim_Hunter)\
**Post date:** [12 March 2006 21:23 UTC](https://rubytalk.org/t/array-element-access/25698/8 "2006-03-12T21:23:45Z")

</div>

Daniel Harple 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
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> 
> require 'enumerator'  
> a = (1..10).to\_a  
> p a.enum\_for(:each\_slice, 2).collect { |m| m[1] } # -\> [2, 4, 6, 8, 10]
> 
> -- Daniel

enumerator is handy but it seems a bit like overkill for a task that can be done with the builtin classes:

irb(main):009:0\> ary = [1,2,3,4,5,6]  
=\> [1, 2, 3, 4, 5, 6]  
irb(main):010:0\> ary2 =   
=\>   
irb(main):011:0\> ary.each\_with\_index { |a, i| ary2 \<\< a if i % 2 == 1 }  
=\> [1, 2, 3, 4, 5, 6]  
irb(main):012:0\> ary2  
=\> [2, 4, 6]

> **···**
>
> > On Mar 12, 2006, at 9:34 PM, Tomas Fischer wrote:

---

<div class="post-metadata">

**Author:** ![Hans\_Fugal3](https://avatars.discourse-cdn.com/v4/letter/h/34f0e0/32.png) [@Hans\_Fugal3](https://rubytalk.org/u/Hans_Fugal3)\
**Post date:** [12 March 2006 22:08 UTC](https://rubytalk.org/t/array-element-access/25698/9 "2006-03-12T22:08:45Z")

</div>

Daniel Harple 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
> > 
> > --Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> 
> require 'enumerator'  
> a = (1..10).to\_a  
> p a.enum\_for(:each\_slice, 2).collect { |m| m[1] } # -\> [2, 4, 6, 8, 10]
> 
> -- Daniel

I might do it this way:

map , = (1..10).partition{|i| i%2==0}  
map.each {|i| puts a[i]}

or this way

a.each\_with\_index {|a,i| if i%2 == 0; ... ; end}

Depending on my needs and mood

> **···**
>
> > On Mar 12, 2006, at 9:34 PM, Tomas Fischer wrote:

---

<div class="post-metadata">

**Author:** ![W\_James](https://avatars.discourse-cdn.com/v4/letter/w/e274bd/32.png) [@W\_James](https://rubytalk.org/u/W_James)\
**Post date:** [12 March 2006 22:18 UTC](https://rubytalk.org/t/array-element-access/25698/10 "2006-03-12T22:18:44Z")

</div>

Daniel Harple wrote:

> **···**
>
> > On Mar 12, 2006, at 9:34 PM, 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  
> > \>  
> > \> --  
> > \> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).  
> > \>
> > 
> > require 'enumerator'  
> > a = (1..10).to\_a  
> > p a.enum\_for(:each\_slice, 2).collect { |m| m[1] } # -\> [2, 4, 6, 8, 10]
> > 
> > -- Daniel
> 
> f=1  
> [1,2,3,4,5,6].select{f=!f}.each{|x|p x}

---

<div class="post-metadata">

**Author:** ![Adam\_Shelly](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@Adam\_Shelly](https://rubytalk.org/u/Adam_Shelly)\
**Post date:** [13 March 2006 00:00 UTC](https://rubytalk.org/t/array-element-access/25698/11 "2006-03-13T00:00:55Z")

</div>

That's a problem if there are duplicates in the array:

irb(main):012:0\> a=[1,1,2,2,3,3,4,5]  
=\> [1, 1, 2, 2, 3, 3, 4, 5]  
irb(main):013:0\> a.select{|e| a.index(e) %2 == 1}  
=\> [5]  
irb(main):014:0\> s=true  
=\> true  
irb(main):015:0\> a.select{s=!s}  
=\> [1, 2, 3, 5]  
irb(main):016:0\>

-Adam

> **···**
>
> On 3/12/06, Luc Heinrich \<luc@honk-honk.com\> wrote:
> 
> > On 12 mars 06, at 21:34, Tomas Fischer wrote:
> > 
> > \> 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?
> > 
> > irb(main):001:0\> a = [1,2,3,4,5,6]  
> > =\> [1, 2, 3, 4, 5, 6]  
> > irb(main):002:0\> a.select {|e| a.index(e) % 2 == 1 }  
> > =\> [2, 4, 6]
> > 
> > --

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [13 March 2006 23:33 UTC](https://rubytalk.org/t/array-element-access/25698/12 "2006-03-13T23:33:59Z")

</div>

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](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

> **···**
>
> On Tue, 14 Mar 2006, Mike Austin wrote:
> 
> --  
> David A. Black (dblack@wobblini.net)  
> Ruby Power and Light, LLC ([http://www.rubypowerandlight.com](http://www.rubypowerandlight.com))
> 
> "Ruby for Rails" chapters now available  
> from Manning Early Access Program! [Ruby for Rails](http://www.manning.com/books/black)

---

<div class="post-metadata">

**Author:** ![Luc\_Heinrich](https://avatars.discourse-cdn.com/v4/letter/l/958977/32.png) [@Luc\_Heinrich](https://rubytalk.org/u/Luc_Heinrich)\
**Post date:** [13 March 2006 08:59 UTC](https://rubytalk.org/t/array-element-access/25698/13 "2006-03-13T08:59:19Z")

</div>

Yep, good catch.

> **···**
>
> On 13 mars 06, at 01:00, Adam Shelly wrote:
> 
> > That's a problem if there are duplicates in the array:
> 
> --  
> Luc Heinrich - luc@honk-honk.com - [http://www.honk-honk.com](http://www.honk-honk.com)

---

<div class="post-metadata">

**Author:** ![Mike\_Austin4](https://avatars.discourse-cdn.com/v4/letter/m/fbc32d/32.png) [@Mike\_Austin4](https://rubytalk.org/u/Mike_Austin4)\
**Post date:** [13 March 2006 23:53 UTC](https://rubytalk.org/t/array-element-access/25698/14 "2006-03-13T23:53:45Z")

</div>

dblack@wobblini.net wrote:

> 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](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

> **···**
>
> > On Tue, 14 Mar 2006, Mike Austin wrote:

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [14 March 2006 01:08 UTC](https://rubytalk.org/t/array-element-access/25698/15 "2006-03-14T01:08:49Z")

</div>

Hi --

> **···**
>
> On Tue, 14 Mar 2006, Mike Austin wrote:
> 
> > dblack@wobblini.net wrote:
> > 
> > > Hi --
> > > 
> > > On Tue, 14 Mar 2006, Mike Austin wrote:
> > > 
> > > > 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](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](http://www.rubypowerandlight.com))
> 
> "Ruby for Rails" chapters now available  
> from Manning Early Access Program! [http://www.manning.com/books/black](http://www.manning.com/books/black)
