Sebastian
Sebastian

Reputation: 2204

Manage gem dependencies based on Ruby version

I'm working on a gem that works on Ruby 1.9.3, but installing an up-to-date Gemfile works only on Ruby 2.2. Is there a way of separating dependencies based on Ruby versions?

I've seen this approach in the past:

pry_debugger = RUBY_VERSION < '2.0.0' ? 'pry-debugger' : 'pry-byebug'
spec.add_development_dependency pry_debugger

Or should I just consider supporting one Ruby version (let's say 2.0) and find the supported gems? What's the easiest way of finding what gems work on my local Ruby version?

Upvotes: 2

Views: 1017

Answers (1)

Ginty
Ginty

Reputation: 3501

Logic like you show will work fine for development dependencies since it will be evaluated at bundle install time.

It will not work for runtime dependencies since the logic will be evaluated at gem-build time and not when the gem runs in another environment. RubyGems does not provide a way for you to specify different gems based on the runtime environment, so the only way you could support that would be to release two differently named versions of your gem with different gemspecs.

Personally, I don't see the point of putting development dependencies in the .gemspec, so I always just add these to my Gemfile and reserve the gemspec for runtime dependencies only. This separates the concerns better and makes it clear that gem selection logic can be used in the Gemfile, but not in the gemspec.

Upvotes: 4

Related Questions