Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Nuby Array questions - each iterates nested arrays?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Ed Howland  
View profile  
 More options Oct 26 2005, 1:16 pm
Newsgroups: comp.lang.ruby
From: Ed Howland <ed.howl...@gmail.com>
Date: Thu, 27 Oct 2005 02:16:39 +0900
Local: Wed, Oct 26 2005 1:16 pm
Subject: Nuby Array questions - each iterates nested arrays?
a=[[1, 2, 3], [4, 5, 6]]
=> [[1, 2, 3], [4, 5, 6]]
a.length
=> 2
a.each {|x| puts x}
1
2
3
4
5
6
=> [[1, 2, 3], [4, 5, 6]]
a.each_index {|x| puts x}
0
1

Why does each traverse into the inner arrays as well (tried it with 3
and 4 deep?)
Array#length seems to be correct to me and Array#each_index
This seems to happen to collect!, and I'd gather other methods as well.

Convenient, if you want depth first traversal on all arrays. but what
if you just want to iterate in a shallow manner?

Thanks
Ed


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peter Ertl  
View profile  
 More options Oct 26 2005, 1:22 pm
Newsgroups: comp.lang.ruby
From: "Peter Ertl" <pe...@gmx.org>
Date: Thu, 27 Oct 2005 02:22:44 +0900
Local: Wed, Oct 26 2005 1:22 pm
Subject: Re: Nuby Array questions - each iterates nested arrays?
a.flatten.each { |x| p x}

:-)


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Berger, Daniel  
View profile  
 More options Oct 26 2005, 1:23 pm
Newsgroups: comp.lang.ruby
From: "Berger, Daniel" <Daniel.Ber...@qwest.com>
Date: Thu, 27 Oct 2005 02:23:07 +0900
Local: Wed, Oct 26 2005 1:23 pm
Subject: Re: Nuby Array questions - each iterates nested arrays?

Actually, it doesn't.  It just looks that way because of the way 'puts'
works with arrays.  Try 'ruby -e "puts [1,2,3]"' to see what I mean.
See the docs on IO.puts for more details.

To see what's really happening replace 'puts' with 'p' (inspect) in your
example.

Regards,

Dan


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peter Ertl  
View profile  
 More options Oct 26 2005, 1:27 pm
Newsgroups: comp.lang.ruby
From: "Peter Ertl" <pe...@gmx.org>
Date: Thu, 27 Oct 2005 02:27:34 +0900
Local: Wed, Oct 26 2005 1:27 pm
Subject: Re: Nuby Array questions - each iterates nested arrays?
another possible solution:

class Array
  def each_recursive(&block)
    each do |x|
      if Enumerable === x
        x.each_recursive(&block)
      else
        yield x
      end
    end
  end
end

a=[[1, 2, 3], [4, 5, 6]]
a.each_recursive { |x| p x}


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Markus Koenig  
View profile  
 More options Oct 27 2005, 2:26 pm
Newsgroups: comp.lang.ruby
From: Markus Koenig <mar...@stber-koenig.de>
Date: Thu, 27 Oct 2005 20:26:42 +0200
Local: Thurs, Oct 27 2005 2:26 pm
Subject: Re: Nuby Array questions - each iterates nested arrays?

Ed Howland wrote:
> Why does each traverse into the inner arrays as well (tried it with 3
> and 4 deep?)

This does not happen, but the puts makes it seem so. What really
happens is this:

=> puts [1, 2, 3]
1
2
3
=> puts [4, 5, 6]
4
5
6

Try p though:

=> [[1, 2, 3], [4, 5, 6]].each {|x| p x}
[1, 2, 3]
[4, 5, 6]

Greetings,
Markus


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google