# A syntax question about nested 'each' blocks

**URL:** <https://www.ruby-forum.com/t/a-syntax-question-about-nested-each-blocks/85438>\
**Category:** Ruby\
**Created:** [March 22, 2006, 6:05pm UTC](https://www.ruby-forum.com/t/a-syntax-question-about-nested-each-blocks/85438 "2006-03-22T18:05:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 22, 2006, 6:05pm UTC](https://www.ruby-forum.com/t/a-syntax-question-about-nested-each-blocks/85438/1 "2006-03-22T18:05:05Z")

</div>

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)

}

```

}

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 22, 2006, 6:18pm UTC](https://www.ruby-forum.com/t/a-syntax-question-about-nested-each-blocks/85438/2 "2006-03-22T18:18:17Z")

</div>

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

> **[Hey Daddio!](http://www.softiesonrails.com)**
>
> Home of Hey Daddio Software

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 22, 2006, 6:38pm UTC](https://www.ruby-forum.com/t/a-syntax-question-about-nested-each-blocks/85438/3 "2006-03-22T18:38:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 22, 2006, 6:54pm UTC](https://www.ruby-forum.com/t/a-syntax-question-about-nested-each-blocks/85438/4 "2006-03-22T18:54:08Z")

</div>

… 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.)

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 22, 2006, 6:39pm UTC](https://www.ruby-forum.com/t/a-syntax-question-about-nested-each-blocks/85438/5 "2006-03-22T18:39:26Z")

</div>

… I meant:

last\_name = Array.new[] %w{ Groster Riplaid Pewley Lundrund Banks}

It does work for me if I leave out Array.new[]

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 22, 2006, 7:04pm UTC](https://www.ruby-forum.com/t/a-syntax-question-about-nested-each-blocks/85438/6 "2006-03-22T19:04:03Z")

</div>

removed\_email\_address@domain.invalid 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
```

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 22, 2006, 7:50pm UTC](https://www.ruby-forum.com/t/a-syntax-question-about-nested-each-blocks/85438/7 "2006-03-22T19:50:44Z")

</div>

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 W.
