Skip to content

Instantly share code, notes, and snippets.

@samsm
Created May 16, 2017 17:50
Show Gist options
  • Save samsm/f407d5102ed0313cf03f369b07dadbdd to your computer and use it in GitHub Desktop.
Save samsm/f407d5102ed0313cf03f369b07dadbdd to your computer and use it in GitHub Desktop.
A workaround for the gemname/version.rb practice that isn't broken exactly but I don't care for it.
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
# require "something/version" <- hells no
Gem::Specification.new do |spec|
spec.name = "something"
spec.version = "1.2.3" # <- makes sense here, right?
# ... gemspec guts ...
end
module Something
# But what if I want Something::VERSION in the code?
# No problem:
def self.const_missing(constant)
if constant == :VERSION
require "rubygems"
spec_location = File.expand_path("../../something.gemspec", __FILE__)
spec = Gem::Specification::load(spec_location)
const_set(:VERSION, spec.version.to_s)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment