makandra is responsible for maintaining about 75 Ruby projects. These projects use a large number of different versions for Ruby, Rails and many gems. To be able to switch between projects easily, we must control every dependency our applications has.
Goals
- Understand what a gem is
- Learn how to install a gem
- Be able to look into gem code
- locally
- on GitHub
- Learn about Bundler
- Why is it necessary:
- Managing versions and dependencies
- Defining a consistent project environment
- What is the difference between
bundle install
andbundle update
? - What does
bundle exec
do, why is it necessary? - What does the file
Gemfile.lock
do?
- Why is it necessary:
- rbenv
- Understand why we need rbenv
Resources
- Working with Gems Show archive.org snapshot : Tutorial
- RubyGems.org Show archive.org snapshot : This central gem registry is a great place to look up gems with their dependencies and version history.
- Bundler overview and workflow Show archive.org snapshot
- How to update a single gem conservatively: A major bundler caveat
- How to manage versions in a Gemfile
- Ruby Toolbox Show archive.org snapshot : A great place to find popular gems for a particular problem
- Brief introduction to rbenv
Exercises
Working with rbenv
- Use rbenv to install an older Ruby version, like 2.5.
- In an earlier lesson you built a searchable address book.
- Freeze that program's Ruby version to 2.5 by adding an
.ruby-version
file. - Open a new terminal and check your default Ruby version (
ruby -v
) - Now
cd
into the directory for the address book app. Observe howrbenv
has automatically switched to the Ruby version from your.ruby-version
. - Commit and push your changes.
Tip
RubyMine seems to only parse the
.ruby-version
file when you're opening a project for the first time. When you're changing the.ruby-version
of an existing project, RubyMine may not automatically detect that you want to use another version.If this is the case, go to File / Settings / Languages & Frameworks / Ruby SDK & Gems and select the new Ruby version.
Working with Bundler
- In an earlier lesson you built a searchable address book.
- Add a
Gemfile
to that repository and let bundler create aGemfile.lock
. It won't have any dependencies at first. - Commit the
Gemfile
andGemfile.lock
. - Use Bundler to install the
Faker
Show archive.org snapshot
gem in the exact version
2.10.0
. - Observe the changes Bundler made to your
Gemfile.lock
. Compare these changes to faker's dependencies on rubygems.org Show archive.org snapshot . - In your program, use Faker to create 100 contacts with fake data.
- Commit your changes.
- Use Bundler to upgrade Faker to the latest possible version that runs with Ruby 2.5.
- Observe the changes Bundler made to your
Gemfile.lock
during the upgrade. - Commit and push your changes.
How gems are made
- Look at Faker's GitHub repository Show archive.org snapshot .
- Browse through the code and get an idea how it works (you don't have to read or understand it completely). You can either use GitHub's code browser or clone the repository and open it in your code editor.
- Look at Faker's
.gemspec
file in the project's root directory. Understand what it says and how it feeds the faker page on rubygems.org Show archive.org snapshot .
Posted by Henning Koch to makandra Curriculum (2015-07-07 14:59)