# Could someone explain this CGI problem?

**URL:** <https://www.ruby-forum.com/t/could-someone-explain-this-cgi-problem/88879>\
**Category:** Ruby\
**Created:** [January 3, 2006, 2:30pm UTC](https://www.ruby-forum.com/t/could-someone-explain-this-cgi-problem/88879 "2006-01-03T14:30:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lglglg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/dec6dc/32.png) [@lglglg](https://www.ruby-forum.com/u/lglglg)\
**Post date:** [January 3, 2006, 2:30pm UTC](https://www.ruby-forum.com/t/could-someone-explain-this-cgi-problem/88879/1 "2006-01-03T14:30:57Z")

</div>

the following code sequence

cgi.div{  
1…3.times{ |i|  
cgi.div{  
“hello from div”  
}  
}  
}

produces

 1..3

but i wanted to produce

 huhu 

 huhu 

 huhu 

could someone point me how to get the right result ?

---

<div class="post-metadata">

**Author:** ![lglglg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/dec6dc/32.png) [@lglglg](https://www.ruby-forum.com/u/lglglg)\
**Post date:** [January 3, 2006, 2:46pm UTC](https://www.ruby-forum.com/t/could-someone-explain-this-cgi-problem/88879/2 "2006-01-03T14:46:18Z")

</div>

Hi –

On Tue, 3 Jan 2006, lg wrote:

> produces
> 
> huhu 
> 
> huhu 
> 
> could someone point me how to get the right result ?

1…3.times is being parsed as: 1…(3.times). Since times returns its  
receiver, that’s the same as: 1…3

Try this:

cgi.div {  
(1…3).map {  
cgi.div {  
“hello from div” # or “huhu”, or whatever  
}  
}  
}

You could also do:

cgi.div { cgi.div { “hello from div” } \* 3 }

David

–  
David A. Black  
removed\_email\_address@domain.invalid

“Ruby for Rails”, from Manning Publications, coming April 2006!

> **[Ruby for Rails](https://www.manning.com/books/ruby-for-rails)**
>
> NEWER EDITION AVAILABLE
> 
> The Well-Grounded Rubyist, Second Edition is now available. An eBook of the previous edition, The Well-Grounded Rubyist is included at no additional cost when you buy the revised edition! 
> 
> 
> Ruby for Rails helps Rails...

---

<div class="post-metadata">

**Author:** ![lglglg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/dec6dc/32.png) [@lglglg](https://www.ruby-forum.com/u/lglglg)\
**Post date:** [January 3, 2006, 2:55pm UTC](https://www.ruby-forum.com/t/could-someone-explain-this-cgi-problem/88879/3 "2006-01-03T14:55:45Z")

</div>

> produces
> 
> 1..3

This is, because the 1…3… statement returns “1…3” and this is  
what cgi gets. Try it with irb.

perhaps this works (untested)

> cgi.div{  
> temp\_string=""  
> 1…3.times{ |i|  
> temp\_string \<\< cgi.div{ “hello from div” }  
> }  
> temp\_string  
> }

i.e. it collects the divs in a temporary string and gives that to the  
outer div.

Patrick

---

<div class="post-metadata">

**Author:** ![lglglg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/dec6dc/32.png) [@lglglg](https://www.ruby-forum.com/u/lglglg)\
**Post date:** [January 3, 2006, 3:11pm UTC](https://www.ruby-forum.com/t/could-someone-explain-this-cgi-problem/88879/4 "2006-01-03T15:11:40Z")

</div>

Hey chaps,

I’d just like to check that the following uses methods and that no  
classes or instance variables are created. I see that there are no  
@instance\_variables…  
Where’s the object? What class is being used?  
Am I correct in saying that say\_goodnight is a method, where (name) is  
the parameter for it?

#!/usr/bin/ruby

# Tue Dec 27 15:42:59 GMT 2005

# from page 13 of the pick-axe book

def say\_goodnight(name)  
“Good night, #{name.capitalize}”  
# we use the output of the last result - this save time  
end

# Time for bed…

puts say\_goodnight(‘jayeola’)  
puts say\_goodnight(‘john-Boy’)  
puts say\_goodnight(‘mary-Ellen’)  
puts say\_goodnight(‘mary-loo’)  
puts say\_goodnight(‘silly-slapper’)  
puts say\_goodnight(‘hex-editor’)

---

<div class="post-metadata">

**Author:** ![lglglg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/dec6dc/32.png) [@lglglg](https://www.ruby-forum.com/u/lglglg)\
**Post date:** [January 3, 2006, 4:45pm UTC](https://www.ruby-forum.com/t/could-someone-explain-this-cgi-problem/88879/5 "2006-01-03T16:45:56Z")

</div>

thanks - this was the hint i needed !

regards,

lars

---

<div class="post-metadata">

**Author:** ![lglglg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/dec6dc/32.png) [@lglglg](https://www.ruby-forum.com/u/lglglg)\
**Post date:** [January 4, 2006, 1:18pm UTC](https://www.ruby-forum.com/t/could-someone-explain-this-cgi-problem/88879/6 "2006-01-04T13:18:49Z")

</div>

> I’d just like to check that the following uses methods and that no classes  
> or instance variables are created. I see that there are no  
> @instance\_variables…
> 
> Where’s the object?

try  
ruby -e ‘p self’  
(as if executing a ruby file containing “p self” and nothing else)  
it prints “main”.  
Apparently the object sees itself as main. ok.

> What class is being used?

do  
ruby -e ‘p self.class’  
it prints “Object”  
so main is an Object. tadaa!  
(actually, it is a bit more than “just” an Object, but you can figure  
that  
out yourself when you learn about Modules and mixin)

What should you learn from this? Everything is an object. Really. But if  
you  
want to do some simple procedural stuff, the objects do not get in your  
way.  
They can be, as you experienced, completely invisible. That’s one of  
many,  
many reasons I like Ruby so much 🙂

> Am I correct in saying that say\_goodnight is a method, where (name) is the  
> parameter for it?

absolutely correct

> #!/usr/bin/ruby
> 
> # Tue Dec 27 15:42:59 GMT 2005
> 
> # from page 13 of the pick-axe book
> 
> def say\_goodnight(name)  
> “Good night, #{name.capitalize}”  
> # we use the output of the last result - this save time  
> end  
> [snip]
