# Testing for ruby 1.9 in extension code

**URL:** <https://www.ruby-forum.com/t/testing-for-ruby-1-9-in-extension-code/153042>\
**Category:** Ruby\
**Created:** [November 11, 2008, 1:35pm UTC](https://www.ruby-forum.com/t/testing-for-ruby-1-9-in-extension-code/153042 "2008-11-11T13:35:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![absolute-beginner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@absolute-beginner](https://www.ruby-forum.com/u/absolute-beginner)\
**Post date:** [November 11, 2008, 1:35pm UTC](https://www.ruby-forum.com/t/testing-for-ruby-1-9-in-extension-code/153042/1 "2008-11-11T13:35:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![absolute-beginner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@absolute-beginner](https://www.ruby-forum.com/u/absolute-beginner)\
**Post date:** [November 11, 2008, 6:20pm UTC](https://www.ruby-forum.com/t/testing-for-ruby-1-9-in-extension-code/153042/2 "2008-11-11T18:20:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![absolute-beginner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@absolute-beginner](https://www.ruby-forum.com/u/absolute-beginner)\
**Post date:** [November 11, 2008, 7:10pm UTC](https://www.ruby-forum.com/t/testing-for-ruby-1-9-in-extension-code/153042/3 "2008-11-11T19:10:40Z")

</div>

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/ruby-core/12069](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/12069)

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

---

<div class="post-metadata">

**Author:** ![absolute-beginner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@absolute-beginner](https://www.ruby-forum.com/u/absolute-beginner)\
**Post date:** [November 12, 2008, 4:40pm UTC](https://www.ruby-forum.com/t/testing-for-ruby-1-9-in-extension-code/153042/4 "2008-11-12T16:40:08Z")

</div>

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
