Method for checking syntax

You can create a tempfile and then run

ruby -c filename # for syntax check
ruby -w filename # for warnings
ruby -wc filename # for both

I’d love to learn if there’s a programmatic way to do this within Ruby,
but I haven’t found it in the source code, and I’d rather than bring in
the rubinius melbourne parser.

This is how sandi_meter does it

Open3.capture3(‘ruby -wc’, stdin_data: source)

But that’s potentially pretty buggy on non *NIX OS’s or non-mri rubies.

Does that possibly help? (Does anyone know how to do this within ruby?)

On Thu, Dec 12, 2013 at 1:38 PM, Benjamin F. [email protected]
wrote:

This is how sandi_meter does it
Posted via http://www.ruby-forum.com/.

Why wouldn’t that work on windows? Better yet which non-mri ruby are you
referring to?

On Sat, Dec 14, 2013 at 11:01 PM, Stu [email protected] wrote:

Open3.capture3(‘ruby -wc’, stdin_data: source)

But that’s potentially pretty buggy on non *NIX OS’s or non-mri rubies.

Why wouldn’t that work on windows? Better yet which non-mri ruby are you
referring to?

Just the evidence of these bugs are why I’m not sure.

On Wed, Dec 11, 2013 at 11:08 AM, Thyago Barbosa Rodrigues
[email protected] wrote:

true
rescue SyntaxError
false
end

Why insert the throw before the code to be checked? If throw comes first,
shouldn’t it always jump out of the eval before checking src?
Sorry for slowing the conversation down, just seeing an opportunity to learn

It is to ensure that you don’t execute the code just yet. You want
only to check the syntax, not execute it.
eval will parse the code, raising a SyntaxError if something is
syntactically wrong, then start executing it. First thing is the
throw, so it throws, skipping all the rest.

Jesus.

Robert K. wrote in post #1129668:

On Thu, Dec 5, 2013 at 7:09 AM, Asmita C. [email protected]
wrote:

Actually, I want to separate out parsing(for syntax checking) and
execution.
In our Application, we are using eval, instace_eval, class_eval etc.
what i want to do is that parse code block for syntax checking at early
stage and defer the execution at later stage.

Just wrap the piece of code:

irb(main):007:0> code = ‘1 + 2’
=> “1 + 2”
irb(main):008:0> compiled = eval(“lambda { #{code} }”)
=> #<Proc:0x0000060036f988@(eval):1 (lambda)>
irb(main):009:0> compiled.call
=> 3

Now with a broken piece:

irb(main):010:0> code = ‘1 + ’
=> "1 + "
irb(main):011:0> compiled = eval(“lambda { #{code} }”)
SyntaxError: (eval):1: syntax error, unexpected ‘}’
lambda { 1 + }
^
from (irb):11:in eval' from (irb):11 from /usr/bin/irb:12:in

Cheers

robert
Sorry for late update.
Robert,I was trying this as follows

def method method_name, &code_block
code = eval(“lambda {#{code_block}}”)
code.call
end

method :method1 do
sytem(“echo ‘this is method1’”)
end

i am getting following error:-
test1.rb:10:in method': (eval):1:in methode’: compile error
(SyntaxError)
(eval):1: syntax error, unexpected $end
lambda {#Proc:[email protected]:33}
^
from test1.rb:33:in eval' from test1.rb:10:in method’
from test1.rb:33

please help me how could i do this