Forum: Ruby A syntax question about nested 'each' blocks

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.
896f2e4e2fb58082aec6eea74a18ced5?d=identicon&s=25 unknown (Guest)
on 2006-03-22 18:05
(Received via mailing list)
Would someone explain the syntax of nested blocs vis-a-vis the use of
Array.each ?

 I thought that 'lname' would be an element of an Array in the program
below.  But the interpreter (ruby-1.8.4-1.fc3 in Fedora Core 3)  tells
me it is the entire Array.

 first_name = Array.[]( "Joe ","Albert ","Lilly ","Henry ","Becky
","Ray ")
 last_name = Array.[]( "Groster", "Riplaid", "Pewley", "Lundrund",
"Banks" }}

tmp_name = "long string of nothing"

first_name.each { |fname|

     last_name.each { |lname|

          tmp_name = fname
         #  tmp_name = fname + lname   # This produces a syntax error
         #  puts(tmp_name)

         puts( lname.inspect)

    }

}
8217faf2bfdfa7daf10135d41ddd421e?d=identicon&s=25 Jeff Cohen (jeff)
on 2006-03-22 18:18
unknown wrote:
> Would someone explain the syntax of nested blocs vis-a-vis the use of
> Array.each ?

Your code works for me, but I had to first replace the }} with ) at the
end of your last_names definition.  I also removed Array.new which is
unneeded, though you can keep that if you want:

 first_name = [ "Joe ","Albert ","Lilly ","Henry ","Becky ","Ray " ]
 last_name = [ "Groster", "Riplaid", "Pewley", "Lundrund", "Banks" ]

Jeff
www.softiesonrails.com
896f2e4e2fb58082aec6eea74a18ced5?d=identicon&s=25 unknown (Guest)
on 2006-03-22 18:38
(Received via mailing list)
Thanks.  My code works for me too, with your corrections!  But does it
work for you to use

last_name = %w{ Groster Riplaid Pewley Lundrund Banks }

(as in Programming Ruby 1st Ed p 10 )
That's what I had before I mangled it in my experimentations.
896f2e4e2fb58082aec6eea74a18ced5?d=identicon&s=25 unknown (Guest)
on 2006-03-22 18:39
(Received via mailing list)
.... I meant:

last_name = Array.new[] %w{ Groster Riplaid Pewley Lundrund Banks}

It does work for me if I leave out Array.new[]
896f2e4e2fb58082aec6eea74a18ced5?d=identicon&s=25 unknown (Guest)
on 2006-03-22 18:54
(Received via mailing list)
.... or rather
last_name = Array.[] %w { Groster Riplaid Pewley Lundrund Banks }

(I'm beginning to see that Ruby syntax is rather like bash syntax, but
only in that  almost anything you type will at least start to do
something.)
5befe95e6648daec3dd5728cd36602d0?d=identicon&s=25 Robert Klemme (Guest)
on 2006-03-22 19:04
(Received via mailing list)
stephen.tashiro@us.army.mil wrote:
> Thanks.  My code works for me too, with your corrections!  But does it
> work for you to use
>
> last_name = %w{ Groster Riplaid Pewley Lundrund Banks }

Yes.

    robert
10d4acbfdaccb4eee687a428ca00a5d8?d=identicon&s=25 Jim Weirich (weirich)
on 2006-03-22 19:50
unknown wrote:
> .... or rather
> last_name = Array.[] %w { Groster Riplaid Pewley Lundrund Banks }
>
> (I'm beginning to see that Ruby syntax is rather like bash syntax, but
> only in that  almost anything you type will at least start to do
> something.)

Try:

   last_name = Array.[] *%w{ Groster Riplaid Pewley Lundrund Banks }

Notice there is no space between the "%w" and "{".  Also note the use of
the splat operator "*" to flatten the array into the argument list.

Without the splat, it is equivalent to:

    Array[ %w{ a b } ]   =>   Array[ [ 'a', 'b' ] ]

With the splat, it is equivalent to:

    Array[ * %w{ a b } ] =>   Array[ 'a', 'b' ]

HTH

--
-- Jim Weirich
This topic is locked and can not be replied to.