Forum: Ruby testing for ruby 1.9 in extension code

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Alex F. (Guest)
on 2008-11-11 14:35
(Received via mailing list)
Hi

In C extension code, what's the canonical way to test whether the ruby
being compiled against is 1.8 or 1.9?

I've been using ways like

#ifdef RUBY_RUBY_H
... 1.9 code
#endif

But this seems accidental. However I didn't find a RUBY_VERSION constant
in the headers anywhere.

I'd like to submit a patch to SWIG to fix problems it has with the
latest 1.9.1-preview, and wish to use the authoritative way.

thanks
a
Daniel Schömer (Guest)
on 2008-11-11 19:20
(Received via mailing list)
Alex F. wrote:
> In C extension code, what's the canonical way to test whether the ruby
> being compiled against is 1.8 or 1.9?
> [...]
> But this seems accidental. However I didn't find a RUBY_VERSION constant
> in the headers anywhere.
> [...]

Have you looked at version.h?

# head -n12 ruby-1.9-svn/version.h
#define RUBY_VERSION "1.9.0"
#define RUBY_RELEASE_DATE "2008-11-10"
#define RUBY_VERSION_CODE 190
#define RUBY_RELEASE_CODE 20081110
#define RUBY_PATCHLEVEL 0

#define RUBY_VERSION_MAJOR 1
#define RUBY_VERSION_MINOR 9
#define RUBY_VERSION_TEENY 0
#define RUBY_RELEASE_YEAR 2008
#define RUBY_RELEASE_MONTH 11
#define RUBY_RELEASE_DAY 10

Regards,
Daniel
Alex F. (Guest)
on 2008-11-11 20:10
(Received via mailing list)
Daniel Schömer wrote:
> # head -n12 ruby-1.9-svn/version.h
> #define RUBY_VERSION "1.9.0"
> #define RUBY_RELEASE_DATE "2008-11-10"
> #define RUBY_VERSION_CODE 190
> #define RUBY_RELEASE_CODE 20081110
> #define RUBY_PATCHLEVEL 0


Thanks, this looked perfect. I wondered why my 'grep VERSION
include/ruby/*.h' hadn't found this. Turns out version.h is deliberately
not installed for ruby 1.9, although it is in the source tree:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/...

So the correct way is apparently never to test version, but presence of
features. In my case I ended up doing

#ifdef HAVE_RUBY_IO_H
#include "ruby/io.h"
#else
#include "rubyio.h"
#endif

to get around the disappearance of the latter header in 1.9.1.

thanks
a
Robert D. (Guest)
on 2008-11-12 17:40
(Received via mailing list)
May I suggest a different approach, with the fast evolution of Ruby I
would rather not check for the version but for features. (Like e.g.
Object detection vs. Browser detection in Javascript)
E.g.

begin
   Object.method :tap
rescue NameError
   Object.module_eval do
      def tap &blk; instance_eval( &blk ); self end
   end
end

The above example, that I actually used, came to full value when I
upgraded from 8.6 to 8.7 :)

HTH
Robert
--
C'est véritablement utile puisque c'est joli.

Antoine de Saint Exupéry
This topic is locked and can not be replied to.