Bird
Raised Fist0
Ruby on Railsframework~20 mins

Gemfile and dependency management in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Gemfile Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What happens when you run bundle install with a Gemfile.lock present?

You have a Gemfile and a Gemfile.lock in your Rails project. You run bundle install. What does Bundler do?

AIt ignores <code>Gemfile.lock</code> and installs the latest versions allowed by <code>Gemfile</code>.
BIt deletes <code>Gemfile.lock</code> and installs gems fresh from the internet.
CIt installs the exact gem versions listed in <code>Gemfile.lock</code>, ignoring version changes in <code>Gemfile</code>.
DIt only updates gems that are missing, leaving others untouched regardless of <code>Gemfile</code>.
Attempts:
2 left
💡 Hint

Think about how Bundler ensures consistent gem versions across machines.

📝 Syntax
intermediate
2:00remaining
Which Gemfile syntax correctly specifies a gem only for the development environment?

You want to add the gem pry only for development. Which Gemfile snippet is correct?

A
group :dev do
  gem 'pry'
end
Bgem 'pry', group: :development
Cgem 'pry', only: :development
D
group :development do
  gem 'pry'
end
Attempts:
2 left
💡 Hint

Remember the standard way to group gems by environment in Bundler.

🔧 Debug
advanced
2:00remaining
Why does bundle exec rails server fail with 'Could not find gem' error after adding a new gem?

You added a new gem to your Gemfile and ran bundle install. But running bundle exec rails server gives an error: 'Could not find gem ...'. What is the likely cause?

AYou did not run <code>bundle install</code> after adding the gem to Gemfile.
BYou added the gem but did not run <code>bundle update</code> to update Gemfile.lock.
CYou forgot to restart your terminal or shell session after installing the gem.
DYou installed the gem globally instead of using Bundler.
Attempts:
2 left
💡 Hint

Think about how Bundler uses Gemfile.lock to track installed gems.

🧠 Conceptual
advanced
2:00remaining
What is the purpose of the source line in a Gemfile?

In a Gemfile, you often see a line like source 'https://rubygems.org'. What does this line do?

AIt sets the environment variable for gem installation.
BIt tells Bundler where to fetch gems from when installing dependencies.
CIt defines the local path where gems are stored on your machine.
DIt specifies the Ruby version to use for the project.
Attempts:
2 left
💡 Hint

Think about where gems come from when you run bundle install.

❓ state_output
expert
3:00remaining
What is the content of Gemfile.lock after running bundle install with this Gemfile?

Given this Gemfile:

source 'https://rubygems.org'
gem 'rails', '~> 7.0.0'
group :development do
  gem 'pry'
end

You run bundle install on a fresh project. What will Gemfile.lock contain regarding the pry gem?

Ruby on Rails
source 'https://rubygems.org'
gem 'rails', '~> 7.0.0'
group :development do
  gem 'pry'
end
A<p><code>pry</code> and its dependencies are listed under the <code>development</code> group section in <code>Gemfile.lock</code>.</p>
B<p><code>pry</code> is listed under the <code>default</code> group in <code>Gemfile.lock</code>.</p>
C<p><code>pry</code> is not listed anywhere in <code>Gemfile.lock</code> because it's only for development.</p>
D<p><code>pry</code> is listed but without any group information in <code>Gemfile.lock</code>.</p>
Attempts:
2 left
💡 Hint

Consider how Bundler groups gems in the lockfile.

Practice

(1/5)
1. What is the main purpose of a Gemfile in a Rails application?
easy
A. To list all the gems the app needs and manage their versions
B. To write Ruby code for the app's business logic
C. To configure the database connection settings
D. To define routes for the web application

Solution

  1. Step 1: Understand the role of Gemfile

    The Gemfile is a file where you list all the extra tools (called gems) your Rails app needs to work properly.
  2. Step 2: Identify what Gemfile manages

    It also helps keep everyone using the same versions of these gems, so the app runs the same everywhere.
  3. Final Answer:

    To list all the gems the app needs and manage their versions -> Option A
  4. Quick Check:

    Gemfile purpose = list gems and versions [OK]
Hint: Gemfile lists gems and versions for consistent setup [OK]
Common Mistakes:
  • Confusing Gemfile with database config
  • Thinking Gemfile contains app code
  • Mixing Gemfile with routing files
2. Which of the following is the correct syntax to add the gem 'devise' in a Gemfile?
easy
A. gem 'devise'
B. gem devise
C. add_gem 'devise'
D. install 'devise'

Solution

  1. Step 1: Recall Gemfile gem syntax

    In a Gemfile, gems are added using the keyword gem followed by the gem name in quotes.
  2. Step 2: Check each option

    Only gem 'devise' uses the correct syntax: gem 'devise'. Others are invalid Ruby or Gemfile syntax.
  3. Final Answer:

    gem 'devise' -> Option A
  4. Quick Check:

    Correct gem syntax = gem 'name' [OK]
Hint: Use gem 'name' with quotes in Gemfile [OK]
Common Mistakes:
  • Omitting quotes around gem name
  • Using wrong keywords like add_gem or install
  • Writing gem name without quotes
3. Given this Gemfile snippet:
group :development do
  gem 'pry'
end

group :test do
  gem 'rspec-rails'
end

Which gems will be installed when running bundle install --without test?
medium
A. Only 'rspec-rails' gem will be installed
B. Both 'pry' and 'rspec-rails' gems will be installed
C. Only 'pry' gem will be installed
D. No gems will be installed

Solution

  1. Step 1: Understand groups and --without option

    The Gemfile groups gems by environment. The command bundle install --without test skips installing gems in the :test group.
  2. Step 2: Identify which gems are installed

    Since :test group is excluded, 'rspec-rails' is skipped. Only 'pry' in :development group is installed.
  3. Final Answer:

    Only 'pry' gem will be installed -> Option C
  4. Quick Check:

    --without test skips test gems [OK]
Hint: --without skips gems in that group only [OK]
Common Mistakes:
  • Assuming all gems install regardless of groups
  • Confusing group names with gem names
  • Thinking --without installs excluded gems
4. You added gem 'faker' to your Gemfile but running bundle install gives an error. Which of these is the most likely cause?
medium
A. You forgot to run bundle update after adding the gem
B. You did not save the Gemfile after adding the gem
C. You added the gem outside the Gemfile
D. You did not run bundle install after adding the gem

Solution

  1. Step 1: Check common errors when bundle install fails

    If bundle install errors after adding a gem, a common cause is that the Gemfile changes were not saved, so bundler sees no new gem.
  2. Step 2: Evaluate other options

    Running bundle update is not required after adding a gem; bundle install is enough. Adding gem outside Gemfile or not running bundle install contradicts the question.
  3. Final Answer:

    You did not save the Gemfile after adding the gem -> Option B
  4. Quick Check:

    Unsaved Gemfile changes cause bundle install errors [OK]
Hint: Always save Gemfile before running bundle install [OK]
Common Mistakes:
  • Thinking bundle update is always needed
  • Not saving Gemfile after editing
  • Adding gems outside Gemfile file
5. You want to add a gem only for the test environment and ensure it is not loaded in production. Which Gemfile code correctly achieves this?
hard
A. gem 'capybara', only: :test
B. gem 'capybara', group: :test
C. group :production do gem 'capybara' end
D. group :test do gem 'capybara' end

Solution

  1. Step 1: Understand how to limit gem to test environment

    Using a group :test do ... end block in Gemfile ensures gems inside load only in test environment.
  2. Step 2: Check each option's correctness

    group :test do gem 'capybara' end uses the correct group block syntax. Options A and B use invalid options for gem method. group :production do gem 'capybara' end wrongly groups gem under production.
  3. Final Answer:

    group :test do gem 'capybara' end -> Option D
  4. Quick Check:

    Use group :test block to limit gems to test [OK]
Hint: Use group :test block to isolate test gems [OK]
Common Mistakes:
  • Using invalid gem options like group: or only:
  • Placing test gems in production group
  • Not grouping gems by environment
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