Unexpected output in a metaprogramming application

R

RichardOnRails

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.

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
 
H

Harry Kakueki

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.

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

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
 
H

Harry Kakueki

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

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
 
R

RichardOnRails

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

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
 
G

Gregory Brown

Hi,,

I putting together examples I might use in teaching an elementary Ruby
seminar. =A0for 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.

My problem is the spurious final line of output from the show method:
"1256921" from Example 4. =A0I like to learn why that's generated so
that I can eliminate it. =A0I repeated the Example 4 code out of the
"show" context, =A0and 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
=3D> "123"
 
R

RichardOnRails

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 athttp://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.

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"

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
 
R

RichardOnRails

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top