Bird
Raised Fist0
Rubyprogramming~10 mins

Gem versions and constraints in Ruby - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Gem versions and constraints
Start: Define Gemfile
↓
Specify gem with version constraint
↓
Bundler checks installed gems
↓
Compare installed gem versions with constraints
↓
If match
→Use installed gem
↓
If no match
→Install compatible version
↓
Run application with correct gem versions
This flow shows how Ruby's Bundler reads gem version constraints, checks installed gems, and installs or uses the correct versions.
Execution Sample
Ruby
gem 'rails', '~> 6.1.0'
gem 'nokogiri', '>= 1.10', '< 2.0'
This code specifies gem version constraints for rails and nokogiri in a Gemfile.
Execution Table
StepGemVersion ConstraintInstalled VersionCheck ResultAction
1rails~> 6.1.06.1.36.1.3 matches ~> 6.1.0Use installed version
2nokogiri>= 1.10 and < 2.01.11.01.11.0 matches constraintsUse installed version
3nokogiri>= 1.10 and < 2.02.1.02.1.0 does NOT match < 2.0Install compatible version < 2.0
4rails~> 6.1.06.0.06.0.0 does NOT match ~> 6.1.0Install compatible version >= 6.1.0 and < 6.2.0
5----All gems satisfy constraints, bundler finishes
💡 Bundler stops when all gems satisfy their version constraints.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
rails_versionnone6.1.36.1.36.1.36.1.36.1.3
nokogiri_versionnonenone1.11.01.11.01.11.01.11.0
Key Moments - 2 Insights
Why does '~> 6.1.0' allow version 6.1.3 but not 6.2.0?
Because '~> 6.1.0' means any version >= 6.1.0 and < 6.2.0, so 6.1.3 fits but 6.2.0 is too new (see Step 1 and Step 4 in execution_table).
What happens if installed gem version does not meet constraints?
Bundler will install a compatible version that meets the constraints (see Step 3 and Step 4 in execution_table).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what action is taken when nokogiri version 2.1.0 is installed?
AInstall compatible version < 2.0
BUse installed version
CIgnore version constraints
DUninstall nokogiri
💡 Hint
Check Step 3 in the execution_table where nokogiri version 2.1.0 is evaluated.
At which step does bundler decide to install a new rails version?
AStep 2
BStep 4
CStep 1
DStep 5
💡 Hint
Look at Step 4 in execution_table where rails version 6.0.0 does not match constraint.
If the rails version constraint changed to '~> 6.0.0', what would happen at Step 1?
A6.1.3 would match constraint
BBundler would install version 6.0.0
C6.1.3 would not match constraint
DBundler would ignore rails gem
💡 Hint
Recall that '~> 6.0.0' means >= 6.0.0 and < 6.1.0, check if 6.1.3 fits.
Concept Snapshot
Gem versions in Ruby use constraints like '~> 6.1.0' or '>= 1.10, < 2.0'.
Bundler checks installed gems against these constraints.
If installed versions don't match, bundler installs compatible versions.
'~>' means 'pessimistic version constraint' allowing updates in last digit.
Constraints ensure your app uses safe, compatible gem versions.
Full Transcript
This visual execution shows how Ruby's bundler handles gem versions and constraints. We start by defining gems with version rules in a Gemfile. Bundler checks installed gem versions against these rules. If a version matches, bundler uses it. If not, bundler installs a compatible version. For example, '~> 6.1.0' means any version from 6.1.0 up to but not including 6.2.0. The execution table traces each step checking rails and nokogiri gems. Variable tracker shows how gem versions update after each step. Key moments clarify why some versions match constraints and others don't. The quiz tests understanding of these checks and actions. This helps beginners see how gem version constraints control which gem versions run in their Ruby apps.

Practice

(1/5)
1. What does the Ruby gem version constraint ~> 2.3.0 mean?
easy
A. It allows versions >= 2.3.0 and < 2.4.0
B. It allows only version 2.3.0 exactly
C. It allows any version greater than 2.3.0
D. It allows versions < 2.3.0

Solution

  1. Step 1: Understand the meaning of the pessimistic operator

    The ~> operator means "compatible with" the specified version, allowing updates that do not change the leftmost specified digit.
  2. Step 2: Apply to version 2.3.0

    For ~> 2.3.0, it allows versions from 2.3.0 up to but not including 2.4.0.
  3. Final Answer:

    It allows versions >= 2.3.0 and < 2.4.0 -> Option A
  4. Quick Check:

    ~> 2.3.0 = >= 2.3.0 and < 2.4.0 [OK]
Hint: Remember ~> locks last digit, allows patch updates [OK]
Common Mistakes:
  • Thinking ~> means exact version
  • Confusing ~> with >= or >
  • Assuming it allows major version changes
2. Which of the following is the correct syntax to specify a gem version constraint for versions greater than or equal to 1.5.0 but less than 2.0.0 in a Gemfile?
easy
A. gem 'rails', '== 1.5.0', '< 2.0.0'
B. gem 'rails', '~> 1.5.0', '< 2.0.0'
C. gem 'rails', '> 1.5.0', '<= 2.0.0'
D. gem 'rails', '>= 1.5.0', '< 2.0.0'

Solution

  1. Step 1: Understand how to specify multiple version constraints

    In a Gemfile, multiple version constraints are given as separate string arguments after the gem name.
  2. Step 2: Check each option's syntax

    gem 'rails', '>= 1.5.0', '< 2.0.0' correctly uses '>= 1.5.0' and '< 2.0.0' as separate strings. gem 'rails', '~> 1.5.0', '< 2.0.0' uses '~>' which is not combined with another constraint properly. gem 'rails', '== 1.5.0', '< 2.0.0' uses '==' which fixes exact version, not a range. gem 'rails', '> 1.5.0', '<= 2.0.0' uses '>' instead of '>=' and '<=' which is incorrect for the requirement.
  3. Final Answer:

    gem 'rails', '>= 1.5.0', '< 2.0.0' -> Option D
  4. Quick Check:

    Multiple constraints as separate strings = gem 'rails', '>= 1.5.0', '< 2.0.0' [OK]
Hint: Use separate strings for multiple constraints in Gemfile [OK]
Common Mistakes:
  • Combining constraints in one string incorrectly
  • Using == instead of >= for ranges
  • Using wrong operators like > instead of >=
3. Given the Gemfile line gem 'nokogiri', '~> 1.10.0', which of the following versions will Bundler accept when installing?
medium
A. 1.9.0
B. 1.10.5
C. 2.0.0
D. 1.11.0

Solution

  1. Step 1: Interpret the '~> 1.10.0' constraint

    This means versions >= 1.10.0 and < 1.11.0 are allowed.
  2. Step 2: Check each version against the range

    Version 1.9.0 is less than 1.10.0 (not allowed). 1.10.5 is between 1.10.0 and 1.11.0 (allowed). 2.0.0 is greater than 1.11.0 (not allowed). 1.11.0 is equal to the upper bound (not allowed).
  3. Final Answer:

    1.10.5 -> Option B
  4. Quick Check:

    ~> 1.10.0 allows 1.10.x but not 1.11.0 [OK]
Hint: ~> locks last digit, allows patch updates only [OK]
Common Mistakes:
  • Allowing versions equal to upper bound
  • Confusing minor and patch version limits
  • Assuming major version updates allowed
4. You have this Gemfile line: gem 'puma', '~> 5.0'. You want to allow updates up to but not including 5.1.0. What is the correct fix?
medium
A. Change to gem 'puma', '~> 5'
B. Change to gem 'puma', '>= 5.0', '< 6.0'
C. Change to gem 'puma', '~> 5.0.0'
D. No change needed, it already allows up to 5.1.0

Solution

  1. Step 1: Understand what '~> 5.0' allows

    The constraint '~> 5.0' allows versions >= 5.0.0 and < 6.0.0.
  2. Step 2: Apply to the requirement

    To restrict to up to but not including 5.1.0, change to '~> 5.0.0' which allows >= 5.0.0 and < 5.1.0. The other options either keep the loose range or make it invalid.
  3. Final Answer:

    Change to gem 'puma', '~> 5.0.0' -> Option C
  4. Quick Check:

    ~> 5.0.0 = >= 5.0.0 and < 5.1.0 [OK]
Hint: Add .0 to ~> to lock minor version, allow only patches [OK]
Common Mistakes:
  • Assuming ~> 5.0 means < 5.1
  • Using ~> 5 without patch version
  • Not specifying upper bound explicitly
5. You want to specify a gem version constraint that allows any patch update for version 3.2 but prevents minor version updates to 3.3 or higher. Which constraint should you use?
hard
A. gem 'mygem', '>= 3.2.0', '< 3.3.0'
B. gem 'mygem', '>= 3.2.0'
C. gem 'mygem', '~> 3.2'
D. gem 'mygem', '== 3.2.*'

Solution

  1. Step 1: Understand patch vs minor version updates

    Patch updates change the last digit (e.g., 3.2.1), minor updates change the middle digit (e.g., 3.3.0).
  2. Step 2: Analyze each constraint

    gem 'mygem', '>= 3.2.0', '< 3.3.0' explicitly allows >= 3.2.0 and < 3.3.0 (3.2.x only). gem 'mygem', '>= 3.2.0' sets only lower bound, allows 3.3+. gem 'mygem', '~> 3.2' allows >= 3.2.0 < 4.0 (allows 3.3). gem 'mygem', '== 3.2.*' uses invalid syntax.
  3. Step 3: Select the correct constraint

    The explicit range gem 'mygem', '>= 3.2.0', '< 3.3.0' precisely prevents minor updates beyond 3.2.x.
  4. Final Answer:

    gem 'mygem', '>= 3.2.0', '< 3.3.0' -> Option A
  5. Quick Check:

    Explicit range prevents minor version updates [OK]
Hint: Use explicit >= and < to lock minor version [OK]
Common Mistakes:
  • Using ~> 3.2 which allows minor updates
  • Trying invalid wildcard syntax
  • Not specifying upper bound to prevent minor updates
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard