S
Sarah Allen
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
http://www.ultrasaurus.com
http://blazingcloud.net
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
http://www.ultrasaurus.com
http://blazingcloud.net