# Problem using #{...} in eval

**URL:** <https://www.ruby-forum.com/t/problem-using-in-eval/77169>\
**Category:** Ruby\
**Created:** [November 24, 2006, 9:55pm UTC](https://www.ruby-forum.com/t/problem-using-in-eval/77169 "2006-11-24T21:55:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![richard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f0a364/32.png) [@richard](https://www.ruby-forum.com/u/richard)\
**Post date:** [November 24, 2006, 9:55pm UTC](https://www.ruby-forum.com/t/problem-using-in-eval/77169/1 "2006-11-24T21:55:07Z")

</div>

Below is my “show” method followed by some tests. I envision using  
something like this to produce a tutorial for myself. Examples 1 & 2  
demonstrate that “show” accepts a source code string and then displays  
the source code followed by the result one would obtain by executing  
that code in a stand-alone environment.

But Example #3 does not transmit elements of string (identified by a  
pattern) to a succeeding string. However, Example #4 demonstrates that  
code in #3 (even with an abstraction for the number of digits) works  
fine outside of “eval”.

Is this problem the result of a limitation in eval, or is the a way to  
code it so it works?

====== results =========

> ruby Test1.rb  
> a=2; b=3; a\*b  
> =\> 6

hrs=24;  
mins=60;  
"mins/day = " + (hrs\*mins).to\_s  
=\> mins/day = 1440

require ‘bigdecimal’  
require ‘bigdecimal/math’  
include BigMath  
BigMath::PI(6).to\_s =~ /.(.)(.)(.{5})/  
"result = " + “”  
=\> result =

Pi = 3.14159 (to 6 significant digits)

> Exit code: 0

======= code =========  
def show(stmt)  
puts stmt  
puts "=\> " + eval(stmt).to\_s  
puts  
end

#1: Works great  
show(“a=2; b=3; a\*b”)

#2: Works great, too  
show(%Q@  
hrs=24;  
mins=60;  
"mins/day = " + (hrs\*mins).to\_s  
@)

#3: Can’t find the elements of the match  
show(%Q@require ‘bigdecimal’  
require ‘bigdecimal/math’  
include BigMath  
BigMath::PI(6).to\_s =~ /.(.)(.)(.{5})/  
"result = " + “#{$2}#{$1}#{$3}”  
@)

#4: But this can find them just fine:  
require ‘bigdecimal’  
require ‘bigdecimal/math’  
include BigMath  
n=6  
BigMath::PI(n).to\_s =~ /.(.)(.)(.{#{n-1}})/  
puts “Pi = #{$2}#{$1}#{$3} (to #{n} significant digits)”

---

<div class="post-metadata">

**Author:** ![richard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f0a364/32.png) [@richard](https://www.ruby-forum.com/u/richard)\
**Post date:** [November 25, 2006, 1:24pm UTC](https://www.ruby-forum.com/t/problem-using-in-eval/77169/2 "2006-11-25T13:24:57Z")

</div>

Richard wrote:

> #3: Can’t find the elements of the match  
> show(%Q@require ‘bigdecimal’  
> require ‘bigdecimal/math’  
> include BigMath  
> BigMath::PI(6).to\_s =~ /.(.)(.)(.{5})/  
> "result = " + “#{$2}#{$1}#{$3}”  
> @)

You need to escape the # signs with a slash:

show(%Q@require ‘bigdecimal’  
require ‘bigdecimal/math’  
include BigMath  
BigMath::PI(6).to\_s =~ /.(.)(.)(.{5})/  
"result = " + “#{$2}#{$1}#{$3}”  
@)

---

<div class="post-metadata">

**Author:** ![richard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f0a364/32.png) [@richard](https://www.ruby-forum.com/u/richard)\
**Post date:** [November 28, 2006, 3:05am UTC](https://www.ruby-forum.com/t/problem-using-in-eval/77169/3 "2006-11-28T03:05:32Z")

</div>

Hi John,

Thank you very much. Now that you point out I need to escape those  
“pound signs”, it makes sense.

That enabled me to do what I _really_ wanted to do: set a variable “n”  
to the number of significant digits wanted. The result (which doesn’t  
address rounding, which I’ll add):

show(%Q@require ‘bigdecimal’  
require ‘bigdecimal/math’  
include BigMath  
n=20  
BigMath::PI(n).to\_s =~ /.(.)(.)(.{#{n-1}})/  
"result = " + “#{$2}#{$1}#{$3}”  
@)

Again, many thanks for your expert insight.

Best wishes,  
Richard
