Bird
Raised Fist0
Rubyprogramming~20 mins

Gemfile for project dependencies in Ruby - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Gemfile Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Gemfile dependency version resolution?
Given this Gemfile snippet, what version of the 'rails' gem will Bundler install?
source 'https://rubygems.org'
gem 'rails', '~> 6.1.0'
gem 'rails', '>= 6.1.3'
Ruby
source 'https://rubygems.org'
gem 'rails', '~> 6.1.0'
gem 'rails', '>= 6.1.3'
ARails version 6.2.0
BRails version 6.1.3 or higher but less than 6.2.0
CRails version 6.1.0
DBundler will raise a version conflict error
Attempts:
2 left
💡 Hint
The '~>' operator means 'compatible with', and the '>= 6.1.3' means minimum version 6.1.3.
🧠 Conceptual
intermediate
1:30remaining
Which source declaration is correct in a Gemfile?
You want to specify the source for gems in your Gemfile. Which of these is the correct way to declare the source?
Asource 'https://rubygems.org'
Bset_source 'https://rubygems.org'
Cgem source 'https://rubygems.org'
Dsource = 'https://rubygems.org'
Attempts:
2 left
💡 Hint
Look for the keyword used to specify gem sources in Gemfile syntax.
🔧 Debug
advanced
2:00remaining
Why does this Gemfile cause a syntax error?
Examine this Gemfile snippet and identify why it causes a syntax error:
source 'https://rubygems.org'
gem 'nokogiri', '~> 1.12.0'
gem 'puma', '~> 5.0'
gem 'rails', '~> 6.1.0'
end
Ruby
source 'https://rubygems.org'
gem 'nokogiri', '~> 1.12.0'
gem 'puma', '~> 5.0'
gem 'rails', '~> 6.1.0'
end
AThe Gemfile must start with 'gemfile' keyword
BThe gem versions are invalid and cause a syntax error
CMissing 'source' declaration causes syntax error
DThe 'end' keyword is misplaced and causes a syntax error
Attempts:
2 left
💡 Hint
Check if all keywords and blocks are properly opened and closed.
🚀 Application
advanced
2:00remaining
How to specify a gem from a Git repository in Gemfile?
You want to use a gem directly from a GitHub repository instead of RubyGems. Which Gemfile line correctly specifies this?
Agem 'mygem', git: 'https://github.com/user/mygem.git'
Bgem 'mygem', source: 'https://github.com/user/mygem.git'
Cgem 'mygem', repo: 'https://github.com/user/mygem.git'
Dgem 'mygem', url: 'https://github.com/user/mygem.git'
Attempts:
2 left
💡 Hint
Look for the keyword that specifies a git repository in gem declaration.
❓ Predict Output
expert
2:30remaining
How many gems will be installed by Bundler from this Gemfile?
Consider this Gemfile snippet:
source 'https://rubygems.org'
group :development, :test do
  gem 'rspec'
  gem 'pry'
end
group :production do
  gem 'puma'
end
gem 'rails'

How many gems will Bundler install when running in the test environment?
Ruby
source 'https://rubygems.org'
group :development, :test do
  gem 'rspec'
  gem 'pry'
end
group :production do
  gem 'puma'
end
gem 'rails'
A2 gems
B4 gems
C3 gems
D5 gems
Attempts:
2 left
💡 Hint
Bundler installs gems in the default group plus the groups for the current environment.

Practice

(1/5)
1. What is the main purpose of a Gemfile in a Ruby project?
easy
A. To configure the web server
B. To write Ruby program logic
C. To store database credentials
D. To list all the gems the project depends on

Solution

  1. Step 1: Understand the role of Gemfile

    A Gemfile is used to specify which gems (libraries) a Ruby project needs to work properly.
  2. Step 2: Compare options with Gemfile purpose

    Only To list all the gems the project depends on correctly states that Gemfile lists project gem dependencies.
  3. Final Answer:

    To list all the gems the project depends on -> Option D
  4. Quick Check:

    Gemfile purpose = list gems [OK]
Hint: Gemfile lists gems needed for the project [OK]
Common Mistakes:
  • Confusing Gemfile with Ruby code files
  • Thinking Gemfile stores secrets or configs
  • Assuming Gemfile runs the program
2. Which line is the correct syntax to add the gem 'rails' version 7.0.0 in a Gemfile?
easy
A. gem 'rails', '7.0.0'
B. gem rails, '7.0.0'
C. gem 'rails' version '7.0.0'
D. gem 'rails' => '7.0.0'

Solution

  1. Step 1: Recall Gemfile gem syntax

    The correct syntax uses the keyword gem, gem name in quotes, then version as a string separated by a comma.
  2. Step 2: Check each option

    gem 'rails', '7.0.0' matches the correct syntax: gem 'rails', '7.0.0'. Others have syntax errors or wrong keywords.
  3. Final Answer:

    gem 'rails', '7.0.0' -> Option A
  4. Quick Check:

    Gemfile gem syntax = gem 'name', 'version' [OK]
Hint: Use gem 'name', 'version' format in Gemfile [OK]
Common Mistakes:
  • Omitting quotes around gem name
  • Using => instead of comma for version
  • Writing version without quotes
3. Given this Gemfile snippet:
source 'https://rubygems.org'
gem 'nokogiri', '~> 1.12.0'
gem 'puma', '5.0'

What happens when you run bundle install?
medium
A. Installs latest versions of nokogiri and puma
B. Installs nokogiri version exactly 1.12.0 and puma latest version
C. Installs nokogiri version 1.12.x and puma version 5.0
D. Throws an error due to invalid version syntax

Solution

  1. Step 1: Understand version specifiers in Gemfile

    ~> 1.12.0 means any version >=1.12.0 and <1.13.0 (patch updates allowed). '5.0' means exactly version 5.0.
  2. Step 2: Interpret bundle install behavior

    Bundle installs nokogiri in 1.12.x range and puma exactly 5.0, matching Installs nokogiri version 1.12.x and puma version 5.0.
  3. Final Answer:

    Installs nokogiri version 1.12.x and puma version 5.0 -> Option C
  4. Quick Check:

    ~> means compatible version, exact string means exact version [OK]
Hint: ~> means compatible version, exact string means exact version [OK]
Common Mistakes:
  • Thinking '~>' means exact version
  • Assuming '5.0' means latest version
  • Confusing version operators
4. Identify the error in this Gemfile snippet:
source 'https://rubygems.org'
gem 'rails', '6.1.4'
gem 'pg', '~> 1.2.3'
gem 'puma' '5.0'
medium
A. Missing comma between gem name and version in last line
B. Incorrect source URL
C. Version numbers are invalid
D. Gem 'pg' version specifier is wrong

Solution

  1. Step 1: Check syntax of each gem line

    Each gem line should have a comma between gem name and version string.
  2. Step 2: Locate the error

    Last line has gem 'puma' '5.0' missing a comma between 'puma' and '5.0'.
  3. Final Answer:

    Missing comma between gem name and version in last line -> Option A
  4. Quick Check:

    Gemfile gem syntax requires comma [OK]
Hint: Check commas between gem name and version [OK]
Common Mistakes:
  • Omitting commas between gem name and version
  • Assuming source URL is incorrect
  • Misreading version specifiers
5. You want to add a gem only for the development environment in your Gemfile. Which is the correct way to do this?
hard
A. gem 'pry', environment: 'development'
B. group :development do gem 'pry' end
C. gem 'pry' if ENV['RACK_ENV'] == 'development'
D. group 'development' gem 'pry' end

Solution

  1. Step 1: Recall how to specify environment groups in Gemfile

    Use group :environment_name do ... end block to add gems for specific environments.
  2. Step 2: Evaluate each option

    group :development do gem 'pry' end correctly uses group :development do ... end syntax. Others are invalid Ruby or Gemfile syntax.
  3. Final Answer:

    group :development do gem 'pry' end -> Option B
  4. Quick Check:

    Use group blocks for environment-specific gems [OK]
Hint: Use group :env do ... end for environment gems [OK]
Common Mistakes:
  • Trying to add environment as gem option
  • Using if conditions inside Gemfile
  • Incorrect group block syntax
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