Is there a way in Ruby/Rails to execute code that is in a string?

前端 未结 3 695
臣服心动
臣服心动 2020-12-23 16:36

So I have a database of different code samples (read snippets). The code samples are created by users. Is there a way in Rails to execute it?

So for example I have

3条回答
  •  佛祖请我去吃肉
    2020-12-23 16:54

    To the eval answer (which is the right one) I would add: get thee a copy of the Pickaxe Book (either Programming Ruby or Programming Ruby 1.9 depending on your Ruby version) and read the chapter called "Locking Ruby in the Safe." That chapter is all about Ruby's safe levels and tainted objects, and the chapter opens with exactly your use case and why you need to be paranoid about it.

提交回复
热议问题