Is there any way to check the Ruby syntax on a file or string of Ruby
code besides doing something like ruby -v xyz.rb
? Is there a gem
that would help me here?
Thanks in advance,
Adrian M.
Is there any way to check the Ruby syntax on a file or string of Ruby
code besides doing something like ruby -v xyz.rb
? Is there a gem
that would help me here?
Thanks in advance,
Adrian M.
On Nov 7, 2007, at 21:01 , [email protected] wrote:
Is there any way to check the Ruby syntax on a file or string of Ruby
code besides doing something likeruby -v xyz.rb
? Is there a gem
that would help me here?
In emacs you can set up flymake to do syntax checks when idle… but
all it is doing is a ruby -v tmp$$.rb
on the current buffer contents.
On Nov 8, 2007 5:01 AM, [email protected] [email protected] wrote:
Is there any way to check the Ruby syntax on a file or string of Ruby
code besides doing something likeruby -v xyz.rb
? Is there a gem
that would help me here?Thanks in advance,
Adrian M.
This should give you a starting point:
def eval_with_check(str, b = binding)
begin
eval(str, b)
“OK”
rescue SyntaxError => e
“ERROR: #{e}”
end
end
puts eval_with_check(“1+2”)
puts eval_with_check(“this is not valid”)
puts eval_with_check(“RUBY_VERSION”)
You could also look into LoadError and other runtime exceptions (don’t
remember off the top of my head).
Regards,
Sean
Quoth [email protected]:
Is there any way to check the Ruby syntax on a file or string of Ruby
code besides doing something likeruby -v xyz.rb
? Is there a gem
that would help me here?Thanks in advance,
Adrian M.
IIRC there’s some sort of hack you can do with the ‘defined?’ keyword.
But
you’ll have to google around.
HTH,
This looks really good. Thanks!
AEM
On 08/11/2007, [email protected] [email protected] wrote:
Is there any way to check the Ruby syntax on a file or string of Ruby
code besides doing something likeruby -v xyz.rb
? Is there a gem
that would help me here?
From the file sample/test.rb in the Ruby source code distribution:
def valid_syntax?(code, fname)
eval(“BEGIN {return true}\n#{code}”, nil, fname, 0)
rescue Exception
puts $!.message
false
end
Peter
Thank you all!
AEM
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs