Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Dynamic syntax check

Reply
Thread Tools

Dynamic syntax check

 
 
aemadrid@gmail.com
Guest
Posts: n/a
 
      11-08-2007
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

 
Reply With Quote
 
 
 
 
Sean O'Halpin
Guest
Posts: n/a
 
      11-08-2007
On Nov 8, 2007 5:01 AM, http://www.velocityreviews.com/forums/(E-Mail Removed) <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
Ryan Davis
Guest
Posts: n/a
 
      11-08-2007

On Nov 7, 2007, at 21:01 , (E-Mail Removed) 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.


 
Reply With Quote
 
Calamitas
Guest
Posts: n/a
 
      11-08-2007
On 08/11/2007, (E-Mail Removed) <(E-Mail Removed)> 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

 
Reply With Quote
 
aemadrid@gmail.com
Guest
Posts: n/a
 
      11-08-2007
This looks really good. Thanks!

AEM

On Nov 8, 1:41 am, Calamitas <(E-Mail Removed)> wrote:
> On 08/11/2007, (E-Mail Removed) <(E-Mail Removed)> 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



 
Reply With Quote
 
aemadrid@gmail.com
Guest
Posts: n/a
 
      11-08-2007
Thank you all!

AEM

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Syntax Checker that's better than the normal syntax checker Jacob Grover Ruby 5 07-18-2008 05:07 AM
Syntax error? What syntax error? Assignment fo default values? Mark Richards Perl Misc 3 11-18-2007 05:01 PM
Syntax bug, in 1.8.5? return not (some expr) <-- syntax error vsreturn (not (some expr)) <-- fine Good Night Moon Ruby 9 07-25-2007 04:51 PM
[ANN] SqlStatement 1.0.0 - hide the syntax of SQL behind familiarruby syntax Ken Bloom Ruby 3 10-09-2006 06:46 PM
Syntax highligth with textile: Syntax+RedCloth ? gabriele renzi Ruby 2 12-31-2005 02:44 AM



Advertisments