Get Started
Install Ruby with a version manager, scaffold a tiny app, add gems, run tests. The complete first loop.
Ruby — getting started
EXAMPLE
# ===== 1. Install Ruby (version manager) =====
# rbenv (macOS / Linux):
brew install rbenv ruby-build
rbenv init
rbenv install 3.3.0
rbenv global 3.3.0
# asdf (cross-language):
asdf plugin add ruby
asdf install ruby 3.3.0
asdf global ruby 3.3.0
# Windows: RubyInstaller from rubyinstaller.org
ruby --version
gem --version
# ===== 2. Hello, Ruby =====
# hello.rb
puts 'hello, Ruby'
ruby hello.rb
# ===== 3. Bundler =====
gem install bundler
bundle init # creates Gemfile
# Gemfile
source 'https://rubygems.org'
gem 'sinatra'
gem 'rspec', group: :test
bundle install
# ===== 4. A tiny Sinatra app =====
# app.rb
require 'sinatra'
get '/healthz' do
content_type :json
'{"ok":true}'
end
bundle exec ruby app.rb
# Visit http://localhost:4567/healthz
# ===== 5. Tests (RSpec) =====
bundle exec rspec --init
# spec/calc_spec.rb
RSpec.describe 'arithmetic' do
it 'adds' do
expect(1 + 1).to eq(2)
end
end
bundle exec rspec
# ===== 6. Rails (when you want it) =====
gem install rails
rails new shop
cd shop
bin/rails generate scaffold Product name:string price:decimal
bin/rails db:migrate
bin/rails server
# In 30 seconds you have working CRUD.
# ===== 7. Project layout (small lib) =====
# Gemfile
# lib/
# spec/
# bin/
# ===== Patterns to internalise =====
# - Always pin Ruby with .ruby-version + bundler
# - Use bundle exec to avoid PATH surprises
# - RSpec or Minitest; pick one per project
# - frozen_string_literal: true at the top of every file
# ===== Pitfalls =====
# - sudo gem install -> breaks rbenv shims; use bundler instead
# - Running 'ruby foo.rb' when 'bundle exec ruby foo.rb' is needed
# - Mixing global gems with project Gemfile
# - Forgetting to commit Gemfile.lock for apps (commit it for apps; libraries may skip)
Why it matters
Install via rbenv or asdf, bundle init, gem the libraries you need, bundle exec the runner. From there, Sinatra for tiny APIs, Rails when you want batteries, RSpec for tests. Ruby still rewards a quick start more than almost any stack.
Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.
Discussion
Loading…