[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Dynamic syntax check

aemadrid@gmail.com

11/8/2007 4:58:00 AM

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

5 Answers

Sean O'Halpin

11/8/2007 7:40:00 AM

0

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

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

Ryan Davis

11/8/2007 7:42:00 AM

0


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?

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.


Calamitas

11/8/2007 8:42:00 AM

0

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?

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

aemadrid@gmail.com

11/8/2007 7:43:00 PM

0

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


aemadrid@gmail.com

11/8/2007 8:00:00 PM

0

Thank you all!

AEM