2

I need to validate that a given string is valid Ruby syntax, programmatically, using Ruby. I imagine one way I can do this is by running the code in an EVAL statement, and detecting syntax errors that way.

What's a more proper, safer way I can accomplish this?

14
  • Instead of running the code with EVAL, perhaps just run ruby -c? Commented Nov 19, 2018 at 17:07
  • I need to do this programmatically in the controller action that receives the form submission, and I'd prefer not to use programmatically access the filesystem or command line to do so, unless I'm misunderstanding. Thanks for your help. Commented Nov 19, 2018 at 17:13
  • 3
    This sounds extremely dangerous. Is there any way to do this other than by posting and running raw Ruby code? What's the actual objective here? There's really no "safe" way to run arbitrary code. Shopify has made an effort to contain Ruby in a sandbox so you may want to consider that approach. Commented Nov 19, 2018 at 17:26
  • Definitely, executing the code, as in an eval, would be extremely dangerous, which is why I'd rather a different solution. The goal here is to ensure a string has valid ruby syntax, without executing any arbitrary code. Commented Nov 19, 2018 at 17:32
  • 1
    @sawa thank you for clarifying this - I'll remove the rails references from the question. Commented Nov 27, 2018 at 15:47

2 Answers 2

4

Let the code string be code. The standard way is to do something like this:

begin
  RubyVM::InstructionSequence.compile(code)
  nil
rescue Exception => e
  ... # Put code here to return `e` itself, print its message, or whatever you like
end

If an error is raised and is rescued, that error will display the syntax error. If not (and nil is returned), then code is syntactically valid Ruby code (which does not guarantee that it is free of other types of errors).

The comments saying it is dangerous to do, etc, does not seem to make sense.

Sign up to request clarification or add additional context in comments.

2 Comments

Beautiful - this is exactly what I was looking for. I had previously stated in the question that this code string is coming from the front-end via a ruby editor and ajax, which I later removed to simplify the question, but that is probably why there are comments saying the eval approach is dangerous. I didn't want to be executing arbitrary code coming from the internet, so this solution is much better.
Using eval may be dangerous, but the comments that I mentioned are those that do not mention eval but say that doing this is dangerous.
0

I'd consider checking this in the browser with Opal - https://github.com/opal/opal

1 Comment

This is very cool, and I may use it for checking the syntax on the front-end, but I'm mainly looking for a back-end solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.