Dynamic syntax check

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 Madrid

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

···

On Nov 8, 2007 5:01 AM, aemadrid@gmail.com <aemadrid@gmail.com> wrote:

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 Madrid

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 7, 2007, at 21:01 , aemadrid@gmail.com wrote:

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?

Quoth aemadrid@gmail.com:

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 Madrid

IIRC there's some sort of hack you can do with the 'defined?' keyword. But
you'll have to google around.

HTH,

···

--
Konrad Meyer <konrad@tylerc.org> http://konrad.sobertillnoon.com/

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

···

On 08/11/2007, aemadrid@gmail.com <aemadrid@gmail.com> wrote:

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?

This looks really good. Thanks!

AEM

···

On Nov 8, 1:41 am, Calamitas <calamita...@gmail.com> wrote:

On 08/11/2007, aemad...@gmail.com <aemad...@gmail.com> wrote:

> 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?

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