Ruby is a language designed by Yukihiro Matsumoto (Matz) in Japan in 1995, with "developer happiness" as its foremost design principle. Its concise and expressive syntax, powerful metaprogramming capabilities, and excellent compatibility with the Rails framework make it a highly productive choice to this day.
Ruby's Philosophy: The Principle of Least Astonishment
Matz's emphasized Principle of Least Astonishment (POLA) means that code should behave in the way a developer intuitively expects.
# Everything is an object
puts 5.class # Integer
puts "hello".class # String
puts nil.class # NilClass
puts true.class # TrueClass
# Methods can be called on numbers too
puts 5.times.map { |i| i * 2 }.inspect # [0, 2, 4, 6, 8]
puts -5.abs # 5
puts 3.14.ceil # 4
Blocks, Lambdas, and Procs
Ruby's blocks are a powerful mechanism for passing code as arguments. Blocks can be invoked with the yield keyword.
# Block: use do...end or { }
[1, 2, 3].each do |n|
puts n * 2
end
# Proc: store a block as an object
double = Proc.new { |n| n * 2 }
puts double.call(5) # 10
# Lambda: similar to Proc but with different argument checking and return behavior
square = lambda { |n| n ** 2 }
puts square.call(4) # 16
cube = ->(n) { n ** 3 }
puts cube.call(3) # 27
Metaprogramming
One of Ruby's most powerful features. Code can generate or modify other code at runtime.
class Person
attr_accessor :name, :age # auto-generates getter/setter
def initialize(name, age)
@name = name
@age = age
end
def method_missing(method_name, *args)
if method_name.to_s.start_with?("say_")
word = method_name.to_s.sub("say_", "")
puts "#{@name} says: #{word}"
else
super
end
end
end
person = Person.new("Alice", 30)
person.say_hello # Alice says: hello
person.say_goodbye # Alice says: goodbye
Ruby on Rails: Convention over Configuration
Rails' core philosophy of CoC (Convention over Configuration) and DRY (Don't Repeat Yourself) enables rapid development without repetitive configuration work.
# From new Rails project to running server in 3 lines
rails new myapp --database=postgresql
cd myapp
rails server
# Generate full CRUD instantly with scaffolding
rails generate scaffold Article title:string body:text
rails db:migrate
# Model, migration, controller, views, and routes are auto-generated
When to Choose Ruby
- Suitable for: Web app MVPs, startup prototyping, scripting, CLI tools, internal admin tools
- Note: For high concurrency or extreme performance requirements, consider Go or Rust