Gem (Ruby)
A gem is a packaged Ruby library or application distributed through RubyGems. Gems bundle reusable code, metadata, and dependencies so you can install and share functionality with a single command.
Ruby
RubyGems is Ruby's package manager and standard format. You install a gem with gem install name (it downloads from rubygems.org), then require it in your code. Popular gems include rails, rspec, sinatra, nokogiri, and pry — the building blocks of the Ruby ecosystem.
Each gem has a .gemspec describing its name, version, dependencies, and files. Versions follow semantic versioning, and gems can depend on other gems, which RubyGems resolves and installs automatically.
In real projects you rarely run gem install by hand; instead Bundler reads a Gemfile to install a consistent, locked set of gems for the whole team. Gems plus Bundler are how Ruby projects manage dependencies reproducibly.
Syntax
gem install rails
# in code:
require "json"Examples
Ruby
require "json"
data = { name: "Ada", langs: ["ruby"] }
puts JSON.generate(data)Output:
{"name":"Ada","langs":["ruby"]}