Ruby is an elegant, developer-friendly programming language known for its simplicity and productivity. Created by Yukihiro Matsumoto in 1995, Ruby emphasizes programmer happiness and follows the principle of 'least surprise.'
What is Ruby?
Ruby is a dynamic, object-oriented programming language designed for simplicity and productivity. Created by Yukihiro Matsumoto (Matz) in 1995, Ruby emphasizes programmer happiness and follows the principle of "least surprise" - meaning the language behaves in ways that programmers would naturally expect.
Key Features
Object-Oriented Design
Ruby is a pure object-oriented language where everything is an object, including numbers and strings. This creates a consistent and intuitive programming experience.
# Everything is an object
5.times { puts "Hello" }
"hello".upcase
[1, 2, 3].map { |x| x * 2 }
Dynamic Typing
Ruby is dynamically typed, meaning you don't need to declare variable types. The interpreter determines types at runtime.
name = "Alice" # String
age = 25 # Integer
height = 5.6 # Float
is_student = true # Boolean
Blocks and Iterators
Ruby's block syntax makes iteration and functional programming elegant and readable.
# Traditional loop
(1..5).each do |i|
puts i
end
# Functional style
numbers = [1, 2, 3, 4, 5]
squares = numbers.map { |n| n ** 2 }
evens = numbers.select { |n| n.even? }
Popular Frameworks
Ruby on Rails
The most famous Ruby framework, Rails follows the "Convention over Configuration" principle and enables rapid web development.
# Rails controller example
class UsersController < ApplicationController
def index
@users = User.all
end
def show
@user = User.find(params[:id])
end
end
Sinatra
A lightweight web framework for simple applications and APIs.
require 'sinatra'
get '/' do
"Hello, World!"
end
get '/users/:id' do
"User #{params[:id]}"
end
Ruby Gems
Ruby's package manager, RubyGems, provides access to thousands of libraries and tools.
# Install a gem
gem install rails
# Use in your code
require 'json'
require 'net/http'
When to Use Ruby
Web Development
Automation and Scripting
Prototyping
Ruby vs Other Languages
Compared to Python
Compared to JavaScript
Getting Started
Installation
# Using rbenv (recommended)
brew install rbenv
rbenv install 3.2.0
rbenv global 3.2.0
# Using RVM
curl -sSL https://get.rvm.io | bash -s stable
rvm install ruby-3.2.0
First Ruby Program
# hello.rb
puts "Hello, Ruby World!"
# Run with: ruby hello.rb
Best Practices
Code Style
Testing
Ruby continues to be a popular choice for web development, automation, and learning programming due to its elegant syntax, strong community, and powerful frameworks like Rails.