# Unexpected output in a metaprogramming application

**URL:** <https://www.ruby-forum.com/t/unexpected-output-in-a-metaprogramming-application/172792>\
**Category:** Ruby\
**Created:** [July 28, 2009, 5:15am UTC](https://www.ruby-forum.com/t/unexpected-output-in-a-metaprogramming-application/172792 "2009-07-28T05:15:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [July 28, 2009, 5:15am UTC](https://www.ruby-forum.com/t/unexpected-output-in-a-metaprogramming-application/172792/1 "2009-07-28T05:15:47Z")

</div>

Hi,

I putting together examples I might use in teaching an elementary Ruby  
seminar. for the moment, I like to code examples inside a method that  
displays both the code and the result of the code’s execution.

I pasted both the code and the output presented by running the code  
under SciTE at [http://www.pastie.org/561156](http://www.pastie.org/561156).

My problem is the spurious final line of output from the show method:  
“1256921” from Example 4. I like to learn why that’s generated so  
that I can eliminate it. I repeated the Example 4 code out of the  
“show” context, and it executed as I expected, i.e. no “1256921” ws  
generated.

Any ideas?

Thanks in Advance,  
Richard

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [July 28, 2009, 8:09am UTC](https://www.ruby-forum.com/t/unexpected-output-in-a-metaprogramming-application/172792/2 "2009-07-28T08:09:01Z")

</div>

On Tue, Jul 28, 2009 at 12:15 PM,  
RichardOnRails[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> “1256921” from Example 4. I like to learn why that’s generated so  
> that I can eliminate it. I repeated the Example 4 code out of the  
> “show” context, and it executed as I expected, i.e. no “1256921” ws  
> generated.
> 
> Any ideas?
> 
> Thanks in Advance,  
> Richard

I don’t know if this totally fixes your problem, but take a look at  
this.

def show(stmt)  
puts stmt  
puts "=\> "  
eval(stmt).to\_s # Remove the puts  
puts  
end

Harry

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [July 28, 2009, 8:24am UTC](https://www.ruby-forum.com/t/unexpected-output-in-a-metaprogramming-application/172792/3 "2009-07-28T08:24:49Z")

</div>

> Harry

I do not use eval so I can not tell you much about it.  
And I don’t have time to go through your code carefully right now.  
But, do you need #to\_s ?

def show(stmt)  
puts stmt  
puts "=\> "  
eval(stmt) #######  
puts  
end

Harry

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [July 28, 2009, 4:32pm UTC](https://www.ruby-forum.com/t/unexpected-output-in-a-metaprogramming-application/172792/4 "2009-07-28T16:32:59Z")

</div>

On Mon, Jul 27, 2009 at 11:15 PM,  
RichardOnRails[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> “1256921” from Example 4. I like to learn why that’s generated so  
> that I can eliminate it. I repeated the Example 4 code out of the  
> “show” context, and it executed as I expected, i.e. no “1256921” ws  
> generated.

this is because the return value of your eval() code is the last  
statement executed, which is Array#each

> > [1,2,3].each { }.to\_s  
> > =\> “123”

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [July 28, 2009, 3:36pm UTC](https://www.ruby-forum.com/t/unexpected-output-in-a-metaprogramming-application/172792/5 "2009-07-28T15:36:12Z")

</div>

On Jul 28, 2:24 am, Harry K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Harry
> 
> –  
> A Look into Japanese Ruby List in Englishhttp://www.kakueki.com/ruby/list.html

Hi Harry,

Thanks for your response, esp. the Japanese/English site.

I probably don’t need the “puts”. I’ll check this out tomorrow.

Best wishes,  
Richard

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [July 28, 2009, 11:29pm UTC](https://www.ruby-forum.com/t/unexpected-output-in-a-metaprogramming-application/172792/6 "2009-07-28T23:29:14Z")

</div>

On Jul 28, 10:32 am, Gregory B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > under SciTE athttp://www.pastie.org/561156.
> > 
> > > [1,2,3].each { }.to\_s
> 
> =\> “123”

Hi Gregory,

Many thanks for your perfect response. I apologize to you and this  
newsgroup for being so obtuse. Now that you point out the my error,  
it seems so obvious. Isn’t that always the case when the blind have  
their eyesight restored. 🙂

BTW, I love this technique for demoing code because it avoids the  
pitfall of having the code disagree with output because of a typo.

Best wishes,  
Richard

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [July 28, 2009, 11:31pm UTC](https://www.ruby-forum.com/t/unexpected-output-in-a-metaprogramming-application/172792/7 "2009-07-28T23:31:00Z")

</div>

On Jul 28, 9:32 am, RichardOnRails  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > puts  
> > > puts "=\> "
> 
> Thanks for your response, esp. the Japanese/English site.
> 
> I probably don’t need the “puts”. I’ll check this out tomorrow.
> 
> Best wishes,  
> Richard

I didn’t know exactly which “puts” you thought to be problematic. But  
after getting Gregory’s response, my eyes were opened and my guess is  
the puts before the eval is what you were referring to also.

Again thanks and best wishes,  
Richard
