Ruby Gems

Installing a Specific Version

$ gem install rails -v 6.1.4.4

Updating RubyGems Itself

$ gem update --system

Version Constraints

https://guides.rubygems.org/patterns/

  • >= - optimistic - assume all future versions work
  • ~> - pessimistic - allow only the last digit specified to change. '~> 2.2' is the same as ['>= 2.2.0', '< 3.0']
  • != - exclude a version

Development

Creating a gem: https://bundler.io/v2.0/man/bundle-gem.1.html

$ bundle gem my_gem_name

Referencing an unpublished gem locally

https://rossta.net/blog/how-to-specify-local-ruby-gems-in-your-gemfile.html

gem 'gemname', path: '/path/to/gem'

Installing a gem from git

https://bundler.io/guides/git.html#how-to-install-gems-from-git-repositories

gem 'rack', git: 'https://github.com/rack/rack'
gem 'rack', github: 'rack/rack'
gem 'rack', github: 'rack'

Referencing a git commit, tag, or branch of a gem

gem 'nokogiri', git: 'https://github.com/sparklemotion/nokogiri.git', ref: '0bd839d'
gem 'nokogiri', git: 'https://github.com/sparklemotion/nokogiri.git', tag: '2.0.1'
gem 'nokogiri', git: 'https://github.com/sparklemotion/nokogiri.git', branch: 'rack-1.5'

Publishing a gem

https://guides.rubygems.org/make-your-own-gem/ https://guides.rubygems.org/publishing/

$ gem build
$ gem push mygem-0.1.0.gem

Overriding a published gem with a local copy

https://rossta.net/blog/how-to-specify-local-ruby-gems-in-your-gemfile.html

all apps on system

$ bundle config local.mygem /path/to/mygem

one project

$ bundle config --local local.mygem /path/to/mygem
gem "tacokit", github: "rossta/tacokit", branch: "master"
$ bundle config --delete local.YOUR_GEM_NAME

Opening a gem’s source to view it

$ gem open -e vi [gemname]
# gem pristine [gemname]