Forum: Ruby array element access

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
390b8b63c77bb2c50381648a688a8ecb?d=identicon&s=25 Tomas Fischer (Guest)
on 2006-03-12 21:34
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
9358cc96c46055cd68d4a76a9aefe026?d=identicon&s=25 Daniel Harple (Guest)
on 2006-03-12 21:54
(Received via mailing list)
On Mar 12, 2006, at 9:34 PM, Tomas Fischer wrote:

> Thanks.
> tomas
>
> --
> Posted via http://www.ruby-forum.com/.
>

require 'enumerator'
a = (1..10).to_a
p a.enum_for(:each_slice, 2).collect { |m| m[1] } # -> [2, 4, 6, 8, 10]

-- Daniel
7f891fbe8e3bae7f9fe375407ce90d9d?d=identicon&s=25 Harold Hausman (Guest)
on 2006-03-12 22:07
(Received via mailing list)
> 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
37a3c73ffbf864e4b28f7f2384ee12ce?d=identicon&s=25 Timothy Hunter (tim-hunter)
on 2006-03-12 22:25
(Received via mailing list)
Daniel Harple wrote:
>> is a "ruby way" of doing this.
> 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]
05be5d6610e2c3f1780aa0e39e902e93?d=identicon&s=25 Farrel Lifson (Guest)
on 2006-03-12 22:47
(Received via mailing list)
array.inject(false) do |alternate,element|
  if alternate
     # Your processing
  end
  !alternate
end
Ec594ddcaeae2771ee9d0dd7f0d598ed?d=identicon&s=25 Hans Fugal (Guest)
on 2006-03-12 23:11
(Received via mailing list)
Daniel Harple wrote:
>> is a "ruby way" of doing this.
>
> -- 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
2ee1a7960cc761a6e92efb5000c0f2c9?d=identicon&s=25 William James (Guest)
on 2006-03-12 23:20
(Received via mailing list)
Daniel Harple wrote:
> > is a "ruby way" of doing this.
> 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}
8a85c693f13ef7cb542ef94d2a403d4d?d=identicon&s=25 Luc Heinrich (Guest)
on 2006-03-13 00:33
(Received via mailing list)
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]
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2006-03-13 00:51
(Received via mailing list)
On Mon, 13 Mar 2006, Tomas Fischer wrote:

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

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


   harp:~ > ruby a.rb
   2
   4
   6

regards.

-a
Ddbfebb47432f6599da361df6a135c7c?d=identicon&s=25 Adam Shelly (Guest)
on 2006-03-13 01:03
(Received via mailing list)
On 3/12/06, Luc Heinrich <luc@honk-honk.com> wrote:
>
> --

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
8a85c693f13ef7cb542ef94d2a403d4d?d=identicon&s=25 Luc Heinrich (Guest)
on 2006-03-13 10:02
(Received via mailing list)
On 13 mars 06, at 01:00, Adam Shelly wrote:

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

Yep, good catch.
06db81f02537377c4bb207cdf8fd18ec?d=identicon&s=25 Mike Austin (Guest)
on 2006-03-14 00:25
(Received via mailing list)
Tomas Fischer wrote:
> 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
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-03-14 00:37
(Received via mailing list)
Hi --

On Tue, 14 Mar 2006, Mike Austin wrote:

>> 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
06db81f02537377c4bb207cdf8fd18ec?d=identicon&s=25 Mike Austin (Guest)
on 2006-03-14 00:56
(Received via mailing list)
dblack@wobblini.net wrote:
>>>
>>
> 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
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-03-14 02:10
(Received via mailing list)
Hi --

On Tue, 14 Mar 2006, Mike Austin wrote:

>>>> b=[2,4,6]. How is this done in ruby?
>>>
>> 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
This topic is locked and can not be replied to.