From: Sean O'Halpin Date: 2010-09-07T09:07:00+09:00 Subject: Re: How to require Ruby 1.9 from a required file? On Mon, Sep 6, 2010 at 9:40 PM, Sarah Allen wrote: > Is there any way to require Ruby 1.9 from a required file such that I > can trigger an error message before the main file is parsed? > > Suppose I'm making a DSL that depends on the very nice Ruby 1.9 hash > syntax.  Here's an example: > > app.rb > >    $LOAD_PATH << File.dirname(__FILE__) >    require 'my_library' > >    make_pie apple:2, butter:1, flour:4 >    puts "bye" > > in my_library I want to report an error if we're not running with 1.9, > so I did this: > >    if RUBY_VERSION < "1.9.0" >      abort <<-end_message >        This requires Ruby 1.9 >      end_message >    end > >    def make_pie options >      return unless options.respond_to? :key >      puts "with " + options.keys.join(',') + " we can make a great pie" >    end > > It works great as long as I don't have Ruby 1.9 syntax in the main file, > but when I do, Ruby 1.8.7 will show that error first. > >    $ rvm use 1.9.2-head >    Using ruby 1.9.2 head >    $ ruby app.rb >    with apple,butter,flour we can make a great pie >    bye >    $ rvm use 1.8.7 >    Using ruby 1.8.7 p249 >    $ ruby app.rb >    app.rb:4: syntax error, unexpected tINTEGER, expecting > tSTRING_CONTENT or tSTRING_DBEG or tSTRING_DVAR or tSTRING_END >    make_pie apple:2, butter:1, flour:4 >                    ^ > > I posted the files at:  http://github.com/ultrasaurus/ruby_version_check > > Sure this can be solved with documentation OR changing the syntax of my > DSL to use 1.8.7, but I was hoping for a nicer solution for end users > (who are expected to be kids). > > Thanks in advance, > > Sarah > You can't catch a SyntaxError in same file in which it occurs (if you think about it - your rescue statement has to come after the place where the syntax error has already occurred). You ~can~ catch a SyntaxError in eval'd or loaded code. So, instead of requiring your library, why not use a script loader like this: file: recipe-dsl #!/usr/bin/env ruby def make_pie(params) p params end begin load ARGV[0] rescue SyntaxError => e if RUBY_VERSION < '1.9.0' puts "You need ruby 1.9.x to run this script" else raise end end which you use to run the DSL scripts like so: $ recipe-dsl ~/apple-pie.recipe where apple-pie.recipe contains just your DSL statements: file: apple-pie.recipe make_pie apple: 2, butter: 1, flour: 4 If you try to run this under 1.8.x, you'll get: $ ./recipe-dsl apple-pie.recipe You need ruby 1.9.x to run this script If you really do have a syntax error in your recipe script, that will be reported under 1.9.x. HTH Regards, Sean