Validate Ruby Syntax using Ruby
up vote
0
down vote
favorite
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?
ruby validation syntax
|
show 9 more comments
up vote
0
down vote
favorite
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?
ruby validation syntax
Instead of running the code with EVAL, perhaps just runruby -c
?
– lurker
Nov 19 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.
– choey
Nov 19 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.
– tadman
Nov 19 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.
– choey
Nov 19 at 17:32
1
@sawa thank you for clarifying this - I'll remove the rails references from the question.
– choey
yesterday
|
show 9 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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?
ruby validation syntax
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?
ruby validation syntax
ruby validation syntax
edited yesterday
asked Nov 19 at 17:01
choey
218
218
Instead of running the code with EVAL, perhaps just runruby -c
?
– lurker
Nov 19 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.
– choey
Nov 19 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.
– tadman
Nov 19 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.
– choey
Nov 19 at 17:32
1
@sawa thank you for clarifying this - I'll remove the rails references from the question.
– choey
yesterday
|
show 9 more comments
Instead of running the code with EVAL, perhaps just runruby -c
?
– lurker
Nov 19 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.
– choey
Nov 19 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.
– tadman
Nov 19 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.
– choey
Nov 19 at 17:32
1
@sawa thank you for clarifying this - I'll remove the rails references from the question.
– choey
yesterday
Instead of running the code with EVAL, perhaps just run
ruby -c
?– lurker
Nov 19 at 17:07
Instead of running the code with EVAL, perhaps just run
ruby -c
?– lurker
Nov 19 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.
– choey
Nov 19 at 17:13
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.
– choey
Nov 19 at 17:13
3
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.
– tadman
Nov 19 at 17:26
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.
– tadman
Nov 19 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.
– choey
Nov 19 at 17:32
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.
– choey
Nov 19 at 17:32
1
1
@sawa thank you for clarifying this - I'll remove the rails references from the question.
– choey
yesterday
@sawa thank you for clarifying this - I'll remove the rails references from the question.
– choey
yesterday
|
show 9 more comments
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
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.
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.
– choey
2 days ago
Usingeval
may be dangerous, but the comments that I mentioned are those that do not mentioneval
but say that doing this is dangerous.
– sawa
2 days ago
add a comment |
up vote
0
down vote
I'd consider checking this in the browser with Opal - https://github.com/opal/opal
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.
– choey
2 days ago
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
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.
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.
– choey
2 days ago
Usingeval
may be dangerous, but the comments that I mentioned are those that do not mentioneval
but say that doing this is dangerous.
– sawa
2 days ago
add a comment |
up vote
1
down vote
accepted
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.
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.
– choey
2 days ago
Usingeval
may be dangerous, but the comments that I mentioned are those that do not mentioneval
but say that doing this is dangerous.
– sawa
2 days ago
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
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.
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.
answered Nov 26 at 6:36
sawa
128k27193297
128k27193297
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.
– choey
2 days ago
Usingeval
may be dangerous, but the comments that I mentioned are those that do not mentioneval
but say that doing this is dangerous.
– sawa
2 days ago
add a comment |
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.
– choey
2 days ago
Usingeval
may be dangerous, but the comments that I mentioned are those that do not mentioneval
but say that doing this is dangerous.
– sawa
2 days ago
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.
– choey
2 days ago
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.
– choey
2 days ago
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.– sawa
2 days ago
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.– sawa
2 days ago
add a comment |
up vote
0
down vote
I'd consider checking this in the browser with Opal - https://github.com/opal/opal
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.
– choey
2 days ago
add a comment |
up vote
0
down vote
I'd consider checking this in the browser with Opal - https://github.com/opal/opal
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.
– choey
2 days ago
add a comment |
up vote
0
down vote
up vote
0
down vote
I'd consider checking this in the browser with Opal - https://github.com/opal/opal
I'd consider checking this in the browser with Opal - https://github.com/opal/opal
answered Nov 19 at 17:57
Andrzej Krzywda
1818
1818
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.
– choey
2 days ago
add a 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.
– choey
2 days ago
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.
– choey
2 days ago
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.
– choey
2 days ago
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379441%2fvalidate-ruby-syntax-using-ruby%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Instead of running the code with EVAL, perhaps just run
ruby -c
?– lurker
Nov 19 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.
– choey
Nov 19 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.
– tadman
Nov 19 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.
– choey
Nov 19 at 17:32
1
@sawa thank you for clarifying this - I'll remove the rails references from the question.
– choey
yesterday