Lesson 1 / 25
What Ruby Is and Why Developers Love It
Describe Ruby's philosophy, uses and implementation.
Optimised for programmer happiness
Ruby is a dynamic, object-oriented language created by Yukihiro "Matz" Matsumoto and first released in 1995. Its stated goal is programmer happiness: code that reads naturally, with flexible syntax and few surprises. In Ruby, everything is an object, including numbers, nil and classes themselves, and methods can be called with or without parentheses. Ruby is best known for Ruby on Rails, the web framework behind GitHub, Shopify, Basecamp and many startups, but it is also used for scripting, automation, DevOps tools (Chef, Vagrant and Homebrew's formulae are Ruby) and static site generators (Jekyll). The reference implementation is CRuby (MRI), which includes YJIT, a just-in-time compiler that significantly speeds up real applications, and alternatives include JRuby on the JVM and TruffleRuby. Ruby 3.x added pattern matching, Ractors for parallelism, a Fiber scheduler for non-blocking I/O, endless methods, hash shorthand, Data value objects and the it block parameter. A new Ruby version is traditionally released every Christmas.
Everything is an object
Numbers, strings, nil, arrays and even classes are objects that respond to methods.
A first taste of Ruby
Method calls on literals, blocks and string interpolation.
name = "Asha"
visits = 3
puts "Hello, #{name}! You have visited #{visits} times."
5.times { |i| puts "Line #{i + 1}" } # integers are objects with methods
prices = [49.5, 120, 199]
total = prices.sum
puts "Total: #{total.round(2)}"
puts "Expensive items: #{prices.select { |p| p > 100 }.inspect}"
puts nil.to_a.inspect # even nil is an object: []
puts 42.class # Integer
# run with: ruby hello.rbRead code aloud
Ruby code like orders.select(&:paid?).sum(&:total) is meant to read almost like English. When a line does not read naturally, there is often a more idiomatic way to write it.
Quick check: Which statement about Ruby is true?
- Everything, including numbers and nil, is an object
- Only some values are objects
- Ruby is statically typed
- Ruby cannot be used for web development
Answer
Everything, including numbers and nil, is an object — Ruby has a uniform object model: every value responds to methods.