# Pass def to eval

**URL:** <https://www.ruby-forum.com/t/pass-def-to-eval/151921>\
**Category:** Ruby\
**Created:** [October 27, 2008, 11:57pm UTC](https://www.ruby-forum.com/t/pass-def-to-eval/151921 "2008-10-27T23:57:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![omgitsads](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/dbc845/32.png) [@omgitsads](https://www.ruby-forum.com/u/omgitsads)\
**Post date:** [October 27, 2008, 11:57pm UTC](https://www.ruby-forum.com/t/pass-def-to-eval/151921/1 "2008-10-27T23:57:32Z")

</div>

Hello,

My plan is to have a ruby class with a variety of functions in it and I  
want to be able to call functions from this class using AJAX. I think  
the easiest way to do this then is to pass a cgi parameter named  
function or soemthing with the value being the def I wish to call.

So something like this

// this contained in test.rb

def test(x)  
print x  
end

from .js file

url = test.rb?action=test(‘help’);  
xmlObject.open(“get”,url,true)  
xmlObject.send

anyway something like that and then in test.rb I want to have something  
like

if params.has\_key?(“action”)  
eval(params[“action”])  
end

and hopefully it would return “help”. Please let me know if this is the  
best way to do this and also if I am being unclear I am more than happy  
to clarify.

I have tried something like the above technique and nothing is being  
returned to the XmlObject.

Thank you

---

<div class="post-metadata">

**Author:** ![omgitsads](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/dbc845/32.png) [@omgitsads](https://www.ruby-forum.com/u/omgitsads)\
**Post date:** [October 28, 2008, 1:19am UTC](https://www.ruby-forum.com/t/pass-def-to-eval/151921/2 "2008-10-28T01:19:38Z")

</div>

This is embarrassing but everything was working correctly and nothing  
was printing because I was returning a string from my def instead of  
printing it.

---

<div class="post-metadata">

**Author:** ![omgitsads](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/dbc845/32.png) [@omgitsads](https://www.ruby-forum.com/u/omgitsads)\
**Post date:** [October 28, 2008, 1:54am UTC](https://www.ruby-forum.com/t/pass-def-to-eval/151921/3 "2008-10-28T01:54:04Z")

</div>

On Oct 27, 2008, at 15:57 , Adam H. wrote:

> anyway something like that and then in test.rb I want to have  
> something  
> like
> 
> if params.has\_key?(“action”)  
> eval(params[“action”])  
> end

test.rb?action=system(“rm -rf /”)

---

<div class="post-metadata">

**Author:** ![omgitsads](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/dbc845/32.png) [@omgitsads](https://www.ruby-forum.com/u/omgitsads)\
**Post date:** [October 28, 2008, 2:03am UTC](https://www.ruby-forum.com/t/pass-def-to-eval/151921/4 "2008-10-28T02:03:41Z")

</div>

Ryan D. wrote:

> test.rb?action=system(“rm -rf /”)

Oh thank you Ill try that.
