# How to pull text from database and eval it?

**URL:** <https://www.ruby-forum.com/t/how-to-pull-text-from-database-and-eval-it/61382>\
**Category:** Rails\
**Created:** [May 23, 2006, 4:47pm UTC](https://www.ruby-forum.com/t/how-to-pull-text-from-database-and-eval-it/61382 "2006-05-23T16:47:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![beachguy82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9dc877/32.png) [@beachguy82](https://www.ruby-forum.com/u/beachguy82)\
**Post date:** [May 23, 2006, 4:47pm UTC](https://www.ruby-forum.com/t/how-to-pull-text-from-database-and-eval-it/61382/1 "2006-05-23T16:47:03Z")

</div>

I’m trying to add some executable code to my blog posts. How can I  
include this inside of a blog post and eval it?

Charlie bowman  
[http://www.recentrambles.com](http://www.recentrambles.com)

---

<div class="post-metadata">

**Author:** ![beachguy82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9dc877/32.png) [@beachguy82](https://www.ruby-forum.com/u/beachguy82)\
**Post date:** [May 23, 2006, 4:53pm UTC](https://www.ruby-forum.com/t/how-to-pull-text-from-database-and-eval-it/61382/2 "2006-05-23T16:53:18Z")

</div>

On 5/23/06, Charlie B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m trying to add some executable code to my blog posts. How can I include this inside  
> of a blog post and eval it?

In my controller I have a method that does this:

@content = Content.find( :first, :conditions =\> [ ‘active = 1 AND  
action = ?’, params[:id] ] )

Then in my view I have this:

\<%= render\_template ‘rhtml’, @content.body %\>

---

<div class="post-metadata">

**Author:** ![beachguy82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9dc877/32.png) [@beachguy82](https://www.ruby-forum.com/u/beachguy82)\
**Post date:** [May 23, 2006, 4:56pm UTC](https://www.ruby-forum.com/t/how-to-pull-text-from-database-and-eval-it/61382/3 "2006-05-23T16:56:28Z")

</div>

I’m not sure I understand what you’re getting at. Currently I pull the  
post from the database, pass it to a template and the text is displayed.  
How could I embed a small block of code in that text that instead of  
being displayed, it will be executed? Forgive me if that’s what your  
already showing me.

On Tue, 2006-05-23 at 09:51 -0500, Greg D. wrote:

> \<%= render\_template ‘rhtml’, @content.body %\>

Charlie B.  
[http://www.recentrambles.com](http://www.recentrambles.com)

---

<div class="post-metadata">

**Author:** ![beachguy82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9dc877/32.png) [@beachguy82](https://www.ruby-forum.com/u/beachguy82)\
**Post date:** [May 23, 2006, 5:59pm UTC](https://www.ruby-forum.com/t/how-to-pull-text-from-database-and-eval-it/61382/4 "2006-05-23T17:59:19Z")

</div>

what if I"m only evaling part of the blog post. Would I need to store  
the executable code in a seperate field? I wouldn’t want to eval the  
entire post.

On Tue, 2006-05-23 at 17:37 +0200, Simen wrote:

> > > \<%= render\_template ‘rhtml’, @content.body %\>  
> > > It’s a Ruby method that evaluates a string.  
> > > Charlie B.  
> > > [http://www.recentrambles.com](http://www.recentrambles.com)

---

<div class="post-metadata">

**Author:** ![beachguy82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9dc877/32.png) [@beachguy82](https://www.ruby-forum.com/u/beachguy82)\
**Post date:** [May 23, 2006, 5:37pm UTC](https://www.ruby-forum.com/t/how-to-pull-text-from-database-and-eval-it/61382/5 "2006-05-23T17:37:28Z")

</div>

Charlie B. wrote:

> I’m not sure I understand what you’re getting at. Currently I pull the  
> post from the database, pass it to a template and the text is displayed.  
> How could I embed a small block of code in that text that instead of  
> being displayed, it will be executed? Forgive me if that’s what your  
> already showing me.
> 
> On Tue, 2006-05-23 at 09:51 -0500, Greg D. wrote:
> 
> > \<%= render\_template ‘rhtml’, @content.body %\>
> 
> Charlie B.  
> [http://www.recentrambles.com](http://www.recentrambles.com)

Use eval:

\<%= eval(@your\_code\_block) %\>

It’s a Ruby method that evaluates a string.

---

<div class="post-metadata">

**Author:** ![beachguy82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9dc877/32.png) [@beachguy82](https://www.ruby-forum.com/u/beachguy82)\
**Post date:** [May 23, 2006, 6:13pm UTC](https://www.ruby-forum.com/t/how-to-pull-text-from-database-and-eval-it/61382/6 "2006-05-23T18:13:08Z")

</div>

On 5/23/06, Charlie B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m not sure I understand what you’re getting at. Currently I pull the post from  
> the database, pass it to a template and the text is displayed. How could I embed a  
> small block of code in that text that instead of being displayed, it will be executed?  
> Forgive me if that’s what your already showing me.

render\_template will evaluate any embedded code you include.

---

<div class="post-metadata">

**Author:** ![beachguy82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9dc877/32.png) [@beachguy82](https://www.ruby-forum.com/u/beachguy82)\
**Post date:** [May 23, 2006, 6:22pm UTC](https://www.ruby-forum.com/t/how-to-pull-text-from-database-and-eval-it/61382/7 "2006-05-23T18:22:43Z")

</div>

First put all the Ruby code that shall be executable into a tag:  
  
list = [1, 2, 3]  
list.each do |one|  
print one  
end

So use a simple regex:  
@blog.content = @blog.content.gsub(/\<ruby\>(.\*?)\</ruby\>/) do |match|  
match = eval($1)  
end

–

Judofyr

---

<div class="post-metadata">

**Author:** ![beachguy82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9dc877/32.png) [@beachguy82](https://www.ruby-forum.com/u/beachguy82)\
**Post date:** [May 23, 2006, 6:37pm UTC](https://www.ruby-forum.com/t/how-to-pull-text-from-database-and-eval-it/61382/8 "2006-05-23T18:37:35Z")

</div>

Thanks, that will work perfectly! So simple, it went right over my  
head!

On Tue, 2006-05-23 at 18:22 +0200, Magnus H. wrote:

> match = eval($1)  
> end
> 
> –
> 
> Judofyr

Charlie B.  
[http://www.recentrambles.com](http://www.recentrambles.com)
