# Dynamic syntax check

**URL:** <https://www.ruby-forum.com/t/dynamic-syntax-check/119583>\
**Category:** Ruby\
**Created:** [November 8, 2007, 6:17am UTC](https://www.ruby-forum.com/t/dynamic-syntax-check/119583 "2007-11-08T06:17:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 8, 2007, 6:17am UTC](https://www.ruby-forum.com/t/dynamic-syntax-check/119583/1 "2007-11-08T06:17:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 8, 2007, 8:43am UTC](https://www.ruby-forum.com/t/dynamic-syntax-check/119583/2 "2007-11-08T08:43:57Z")

</div>

On Nov 7, 2007, at 21:01 , removed\_email\_address@domain.invalid 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.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 8, 2007, 8:41am UTC](https://www.ruby-forum.com/t/dynamic-syntax-check/119583/3 "2007-11-08T08:41:00Z")

</div>

On Nov 8, 2007 5:01 AM, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 8, 2007, 9:42am UTC](https://www.ruby-forum.com/t/dynamic-syntax-check/119583/4 "2007-11-08T09:42:29Z")

</div>

Quoth removed\_email\_address@domain.invalid:

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

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

HTH,

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 8, 2007, 8:48pm UTC](https://www.ruby-forum.com/t/dynamic-syntax-check/119583/5 "2007-11-08T20:48:09Z")

</div>

This looks really good. Thanks!

AEM

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 8, 2007, 10:02am UTC](https://www.ruby-forum.com/t/dynamic-syntax-check/119583/6 "2007-11-08T10:02:57Z")

</div>

On 08/11/2007, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 8, 2007, 9:05pm UTC](https://www.ruby-forum.com/t/dynamic-syntax-check/119583/7 "2007-11-08T21:05:41Z")

</div>

Thank you all!

AEM
