Dynamic syntax check

Discussion in 'Ruby' started by aemadrid@gmail.com, Nov 8, 2007.

  1. Guest

    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
     
    , Nov 8, 2007
    #1
    1. Advertising

  2. On Nov 8, 2007 5:01 AM, <> 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
     
    Sean O'Halpin, Nov 8, 2007
    #2
    1. Advertising

  3. Ryan Davis Guest

    On Nov 7, 2007, at 21:01 , 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.
     
    Ryan Davis, Nov 8, 2007
    #3
  4. Calamitas Guest

    On 08/11/2007, <> 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
     
    Calamitas, Nov 8, 2007
    #4
  5. Guest

    This looks really good. Thanks!

    AEM

    On Nov 8, 1:41 am, Calamitas <> wrote:
    > On 08/11/2007, <> 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
     
    , Nov 8, 2007
    #5
  6. Guest

    Thank you all!

    AEM
     
    , Nov 8, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. gabriele renzi
    Replies:
    2
    Views:
    220
    gabriele renzi
    Dec 31, 2005
  2. Ken Bloom
    Replies:
    3
    Views:
    222
  3. Good Night Moon
    Replies:
    9
    Views:
    290
    Rick DeNatale
    Jul 25, 2007
  4. Jacob Grover
    Replies:
    5
    Views:
    325
    Jacob Grover
    Jul 18, 2008
  5. Mark Richards
    Replies:
    3
    Views:
    323
    Tad McClellan
    Nov 18, 2007
Loading...

Share This Page