Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store ruby blocks in db and use them?

Tags:

ruby

block

Basically I want to store a Ruby Block into DB and use them.

One could ask why - reason being, i want my users to be able to post / upload a block code - which could be executed to fetch a desired result for their problem on the data we might have.

like image 934
KannanR Avatar asked Feb 06 '13 07:02

KannanR


2 Answers

So you don't want to store ruby blocks. You want to store code. This is easier. Just store the code as a string. And then later you can eval that string, effectively executing the code.

Now you have to solve a hard problem: how to prevent users from posting malicious code (or how to sandbox them). But that's out of scope here.

like image 177
Sergio Tulentsev Avatar answered Oct 02 '22 23:10

Sergio Tulentsev


First, store your code as a string (the ruby2ruby gem may helps you). When you need to execute this block, retrieve it from DB, and use the "eval" method

like image 38
Miguel Prz Avatar answered Oct 03 '22 00:10

Miguel Prz