Fourth day of the 42 Ruby on Rails Piscine. This module dives into Gems, pieces of reusable Ruby code that you can share and use in different contexts and culminates in first Rails application.
Generated with lsphere.
All exercises run inside a Debian 12 VM managed by Vagrant. Provisioning is split into two steps:
- Ruby — installs rbenv + Ruby 3.3.6 + Bundler
- Rails — installs the Rails gem and sets up the ex03 Hello World app
vagrant up # first boot: provisions everything
vagrant provision # re-run provisioning on a running VM
vagrant ssh # open a shell inside the VMPort 3000 is forwarded from the VM to your host, so http://localhost:3000/ works from your browser once the Rails server is running.
| Exercise | Path | Topic | Description |
|---|---|---|---|
| ex00 | ex00/ |
Your first Gem | A gem that answers the ultimate question of life |
| ex01 | ex01/ |
Open-URI + Nokogiri | Crawls Wikipedia links until it reaches Philosophy |
| ex02 | ex02/taillste/ |
Bundler gem scaffold | A gem with Drum, Beat_box & Clap classes |
| ex03 | ex03/ |
Rails intro | First Rails app displaying "Hello World!" |
Path: ex00/
Gem name: deepthoughth
A gem that defines the Deepthought class with a respond(question) method. When asked "The Ultimate Question of Life, the Universe and Everything" it returns "42" (in green); otherwise it returns "Mmmm i'm bored" (in red).
Dependency: colorize ~> 1.1.0
cd ex00
gem build deepthought.gemspec
gem install deepthoughth-42.0.gemReference: https://guides.rubygems.org/make-your-own-gem/
The
colorizedependency must be declared in the gemspec — otherwise the gem installs but raises aLoadErrorat runtime.
Path: ex01/
Gem name: ft_wikipedia
A gem that defines Ft_wikipedia with a class method search(start). Starting from any Wikipedia article, it repeatedly follows the first valid content link until it reaches the Philosophy article. Raises DeadEnd if a page cannot be fetched and LoopDetected if a cycle is found.
Dependencies: open-uri ~> 0.4.1, nokogiri ~> 1.19.1
cd ex01
gem build ft_wikipedia.gemspec
gem install ft_wikipedia-1.0.gemPath: ex02/taillste/
Gem name: taillste v0.1.0
TDD driven, fix the code to make all tests pass
cd ex02/taillste
bundle install
rake test
gem build taillste.gemspec
gem install taillste-0.1.0.gemPath: ex03/hello_world/
Your first Rails application. The root page (http://localhost:3000/) displays an <h1>Hello World!</h1> heading.
The provisioning script ex03/setup.sh handles everything automatically during vagrant up. To run it manually:
vagrant ssh
bash /vagrant/ex03/setup.shvagrant ssh
cd /vagrant/ex03/hello_world
bundle install
rails server -b 0.0.0.0Then open http://localhost:3000/ in your host browser.
| Step | Action |
|---|---|
| 1 | Installs the rails gem (gem install rails --no-document) |
| 2 | Runs rails new hello_world with minimal flags (SQLite, no mailers/cable/etc.) |
| 3 | Adds root 'home#index' to config/routes.rb |
| 4 | Creates HomeController#index |
| 5 | Creates app/views/home/index.html.erb with <h1>Hello World!</h1> |
ex03/
├── setup.sh # provisioning script
└── hello_world/ # Rails app (created by setup.sh)
├── config/routes.rb # root 'home#index'
├── app/controllers/home_controller.rb
└── app/views/home/index.html.erb # <h1>Hello World!</h1>