0

I recently try to fix some bugs of SciRuby, and found that in extconf.rb it tries to detect the Ruby version and do different work, like this:

if RUBY_VERSION >= '2.0'
  hfiles.puts "#define RUBY_2 1"
end

if RUBY_VERSION < '1.9.3'
  hfiles.puts "#define OLD_RB_SCAN_ARGS"
end

in fact, is there a much better way to implement this? I found this one is weak and not so friendly.

hyaocuk
  • 1
  • 4
  • “I found this one is weak and not so friendly.” How so? – Andrew Marshall Feb 01 '14 at 04:13
  • I think [Here][1] is what you've asked for. [1]: http://stackoverflow.com/questions/1589751/determine-ruby-version-from-within-rails – Emu Feb 01 '14 at 04:43
  • @Emu, yes, I know this, but I want to ask for a better implementation of determination. – hyaocuk Feb 01 '14 at 04:50
  • @Emu, I think that your link formatting only works for q's and a's. See [this](http://meta.stackexchange.com/questions/19756/how-do-comments-work/19757#19757). – Cary Swoveland Feb 01 '14 at 05:42

1 Answers1

0

I'm not sure why it's "weak" and it's pretty clear, although the more common way would be to use trailing conditionals...

hfiles.puts "#define RUBY_2 1"         if RUBY_VERSION >= '2.0'
hfiles.puts "#define OLD_RB_SCAN_ARGS" if RUBY_VERSION <  '1.9.3'

It'll work exactly the same, but it's more compact.

SteveTurczyn
  • 36,057
  • 6
  • 41
  • 53
  • This doesn't appear to be an accurate of comparing a version number ('2.0.0' > '2.0' => true) – eradman Jan 29 '15 at 14:05
  • Good point, @eradman the answer should be edited to use Gem::Version.new or something similar. Feel free to edit or I'll do it myself later. – SteveTurczyn Jan 29 '15 at 14:11
  • I have used RUBY_VERSION[0..2].to_f > 2.0 to detect the major version, but I don't know of a clean way to express the concept of < 1.9.3 – eradman Jan 29 '15 at 14:23
  • This stack overflow addresses this... note the very popular answer http://stackoverflow.com/questions/2051229/how-to-compare-versions-in-ruby – SteveTurczyn Jan 29 '15 at 14:25