A RubyGems repository is a place where Ruby libraries (called gems) are stored and shared. It helps you find and use code others wrote, so you don't have to build everything from scratch.
RubyGems repository
gem install <gem_name> # To use a gem in your Ruby file: require '<gem_name>'
gem install downloads and installs a gem from the RubyGems repository.
require loads the gem in your Ruby program so you can use its features.
gem install rails
require 'json' puts JSON.generate({name: 'Alice', age: 30})
gem install nokogiri require 'nokogiri'
This program installs the colorize gem, loads it, and prints the text 'Hello, world!' in blue color in the terminal.
gem install colorize require 'colorize' puts 'Hello, world!'.colorize(:blue)
You need an internet connection to install gems from the RubyGems repository.
Once installed, gems are saved on your computer and can be used anytime without reinstalling.
Use gem list to see all gems installed on your system.
RubyGems repository stores and shares Ruby libraries called gems.
You install gems using gem install and use them with require.
Gems save you time by letting you reuse code others wrote.
Practice
Solution
Step 1: Understand what RubyGems stores
RubyGems repository is known for storing Ruby libraries called gems.Step 2: Identify the main function
Its main function is to share these gems so developers can reuse code easily.Final Answer:
To store and share Ruby libraries called gems -> Option AQuick Check:
RubyGems = Ruby libraries storage [OK]
- Thinking RubyGems compiles code
- Confusing RubyGems with Ruby interpreter
- Assuming RubyGems runs programs
rails from the RubyGems repository?Solution
Step 1: Recall the correct gem installation syntax
The standard command to install a gem isgem install gem_name.Step 2: Match the command with the gem name
For the gem named rails, the command isgem install rails.Final Answer:
gem install rails -> Option AQuick Check:
Install gem = gem install [OK]
- Swapping 'install' and 'gem' order
- Using 'gem add' instead of 'gem install'
- Adding extra words like 'ruby' before command
require 'json'
puts JSON.generate({name: 'Ruby', type: 'language'})Solution
Step 1: Understand the require statement
The code requires the 'json' gem, which provides JSON methods.Step 2: Analyze JSON.generate output
JSON.generate converts the Ruby hash to a JSON string with keys and values in quotes.Final Answer:
{"name":"Ruby","type":"language"} -> Option CQuick Check:
JSON.generate outputs JSON string [OK]
- Expecting Ruby hash syntax as output
- Confusing JSON.generate with JSON.parse
- Ignoring quotes around keys and values
require 'httparty'
response = HTTParty.get('https://api.example.com/data')
puts response.bodySolution
Step 1: Check if 'httparty' gem is installed
If the gem is not installed, require will fail or HTTParty will be undefined.Step 2: Verify method and usage
HTTParty.get is a valid method, and response.body is valid if response is returned.Final Answer:
The gem 'httparty' is not installed -> Option DQuick Check:
Missing gem causes require failure [OK]
- Assuming require adds gem automatically
- Thinking 'get' method is invalid
- Ignoring gem installation step
Solution
Step 1: Install the gem first
You must install the gem usinggem install colorizebefore using it.Step 2: Require the gem in your script
Userequire 'colorize'to load the gem's code.Step 3: Use the gem's method to color text
Callputs 'text'.colorize(:red)to print red text.Final Answer:
Run gem install, then require, then use colorize method -> Option BQuick Check:
Install -> require -> use gem [OK]
- Requiring gem before installing
- Using 'gem add' instead of 'gem install'
- Trying to use gem methods without require
Ready to prove it?
Pass the test and this pattern moves unknown
