# Evaluating Ruby code that is in a sting, how do I do it?

**URL:** <https://www.ruby-forum.com/t/evaluating-ruby-code-that-is-in-a-sting-how-do-i-do-it/172443>\
**Category:** Rails\
**Created:** [July 21, 2009, 4:47pm UTC](https://www.ruby-forum.com/t/evaluating-ruby-code-that-is-in-a-sting-how-do-i-do-it/172443 "2009-07-21T16:47:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [July 21, 2009, 4:47pm UTC](https://www.ruby-forum.com/t/evaluating-ruby-code-that-is-in-a-sting-how-do-i-do-it/172443/1 "2009-07-21T16:47:47Z")

</div>

Hello there,

I have a code like.

…  
items = entries.map { |entry| content\_tag(“li”, mask, :id =\>  
“#{entry.id}”)}  
…

I want to evaluate the ruby code in the string mask on the fly… it  
content is:  
“#{entry.name} - #{entry.state.name}/#{entry.country.name}”

I have tried “eval mask” but with no success.

thanks,  
David S.

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [July 21, 2009, 6:09pm UTC](https://www.ruby-forum.com/t/evaluating-ruby-code-that-is-in-a-sting-how-do-i-do-it/172443/2 "2009-07-21T18:09:02Z")

</div>

On Jul 21, 3:47 pm, David S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> content is:  
> “#{entry.name} - #{entry.state.name}/#{entry.country.name}”
> 
> I have tried “eval mask” but with no success.

Well eval is the method that evaluates code for you. In what way did  
it not work ?

Fred

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [July 21, 2009, 7:16pm UTC](https://www.ruby-forum.com/t/evaluating-ruby-code-that-is-in-a-sting-how-do-i-do-it/172443/3 "2009-07-21T19:16:08Z")

</div>

When I try to do:

items = entries.map { |entry| content\_tag(“li”, “#{eval(mask)}”, :id =\>  
“#{entry.id}”)}

or

items = entries.map { |entry| content\_tag(“li”, eval(mask), :id =\>  
“#{entry.id}”)}

I get nil as result.

David S.

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [July 23, 2009, 1:57am UTC](https://www.ruby-forum.com/t/evaluating-ruby-code-that-is-in-a-sting-how-do-i-do-it/172443/4 "2009-07-23T01:57:45Z")

</div>

Hello,

> I get nil as result.

I was constructing the string in a wrong way.

The cold should look like this for the :with option:

:with =\> “value + ‘&search=’ + element.value + ‘&field=’ + ‘name’ +  
‘&mask=’ + 'entry.name + " - " +  
entry.state.name+”/"+entry.country.name’"

Regards,

David S.
