Ruby is a dynamic, object-oriented programming language designed for simplicity and productivity. Here are some of its most exciting features:
1. Everything is an Object
In Ruby, every value is an object, even primitive types like integers or nil. This allows you to call methods directly on literals.
Example:
5.times { puts "Ruby!" } # 5 is an Integer object with a `times` method
3.14.floor # => 3 (Float object method)
true.to_s # => "true" (Boolean → String)
nil.nil? # => true (Method to check if object is nil)
2. Elegant and Readable Syntax
Ruby’s syntax prioritizes developer happiness. Parentheses and semicolons are often optional.
Example:
# A method to greet a user (parentheses optional)
def greet(name = "Guest")
puts "Hello, #{name.capitalize}!"
end
greet "alice" # Output: "Hello, Alice!"
3. Blocks and Iterators
Ruby uses blocks (anonymous functions) to create powerful iterators. Use {} for single-line blocks or do...end for multi-line.
Example:
# Multiply even numbers by 2
numbers = [1, 2, 3, 4]
result = numbers.select do |n|
n.even?
end.map { |n| n * 2 }
puts result # => [4, 8]
4. Mixins via Modules
Modules let you share behavior across classes without inheritance.
Example:
module Loggable
def log(message)
puts "[LOG] #{message}"
end
end
class User
include Loggable # Mix in the module
end
user = User.new
user.log("New user created!") # => [LOG] New user created!
5. Metaprogramming
Ruby can generate code at runtime. For example, dynamically define methods.
Example:
class Person
# Define methods like name= and name dynamically
attr_accessor :name, :age
end
person = Person.new
person.name = "Alice"
puts person.name # => "Alice"
6. Duck Typing
Focus on behavior, not type. If it “quacks like a duck,” treat it as a duck.
Example:
def print_length(obj)
obj.length # Works for strings, arrays, or any object with a `length` method
end
puts print_length("Hello") # => 5
puts print_length([1, 2, 3]) # => 3
7. Symbols
Symbols (:symbol) are lightweight, immutable strings used as identifiers.
Example:
# Symbols as hash keys (faster than strings)
config = { :theme => "dark", :font => "Arial" }
puts config[:theme] # => "dark"
# Modern syntax (Ruby 2.0+):
config = { theme: "dark", font: "Arial" }
8. Ruby Set
A set is a Ruby class that helps you create a list of unique items. A set is a class that stores items like an array. But with some special attributes that make it 10x faster in specific situations! All the items in a set are guaranteed to be unique.
What’s the difference between a set & an array?
A set has no direct access to elements:
> seen[3]
(irb):19:in '<main>': undefined method '[]' for #<Set:0x000000012fc34058> (NoMethodError)
But a set can be converted into an array any time you need:
> seen.to_a
=> [4, 8, 9, 90]
> seen.to_a[3]
=> 90
Set: Fast lookup times (with include?)
If you need these then a set will give you a good performance boost, and you won’t have to be calling uniq on your array every time you want unique elements.
Reference: https://www.rubyguides.com/2018/08/ruby-set-class/
Superset & Subset
A superset is a set that contains all the elements of another set.
Set.new(10..40) >= Set.new(20..30)
A subset is a set that is made from parts of another set:
Set.new(25..27) <= Set.new(20..30)
Example:
> seen = Set.new
=> #<Set: {}>
> seen.add(4)
=> #<Set: {4}>
> seen.add(4)
=> #<Set: {4}>
> seen.add(8)
=> #<Set: {4, 8}>
> seen.add(9)
=> #<Set: {4, 8, 9}>
> seen.add(90)
=> #<Set: {4, 8, 9, 90}>
> seen.add(4)
=> #<Set: {4, 8, 9, 90}>
> seen.to_a
=> [4, 8, 9, 90]
> seen.to_a[3]
=> 90
> seen | (1..10) # set union operator
=> #<Set: {4, 8, 9, 90, 1, 2, 3, 5, 6, 7, 10}>
> seen = seen | (1..10)
=> #<Set: {4, 8, 9, 90, 1, 2, 3, 5, 6, 7, 10}>
> seen - (3..4) # set difference operator
=> #<Set: {8, 9, 90, 1, 2, 5, 6, 7, 10}>
set1 = Set.new(1..5)
set2 = Set.new(4..8)
> set1 & set2 # set intersection operator
=> #<Set: {4, 5}>
9. Rich Standard Library
Ruby’s Enumerable module adds powerful methods to collections.
Example:
# Group numbers by even/odd
numbers = [1, 2, 3, 4]
grouped = numbers.group_by { |n| n.even? ? :even : :odd }
puts grouped # => { :odd => [1, 3], :even => [2, 4] }
10. Convention over Configuration
Ruby minimizes boilerplate code with conventions.
Example:
class Book
attr_accessor :title, :author # Auto-generates getters/setters
def initialize(title, author)
@title = title
@author = author
end
end
book = Book.new("Ruby 101", "Alice")
puts book.title # => "Ruby 101"
11. Method Naming Conventions
Method suffixes clarify intent:
?for boolean returns.!for dangerous/mutating methods.
Example:
str = "ruby"
puts str.capitalize! # => "Ruby" (mutates the string)
puts str.empty? # => false
12. Functional Programming Features
Ruby supports Procs (objects holding code) and lambdas.
Example:
# Lambda example
double = lambda { |x| x * 2 }
puts [1, 2, 3].map(&double) # => [2, 4, 6]
# Proc example
greet = Proc.new { |name| puts "Hello, #{name}!" }
greet.call("Bob") # => "Hello, Bob!"
13. IRB (Interactive Ruby)
Test code snippets instantly in the REPL:
$ irb
irb> [1, 2, 3].sum # => 6
irb> Time.now.year # => 2023
14. Garbage Collection
Automatic memory management:
# No need to free memory manually
1000.times { String.new("temp") } # GC cleans up unused objects
15. Community and Ecosystem
RubyGems (packages) like:
- Rails: Full-stack web framework.
- RSpec: Testing framework.
- Sinatra: Lightweight web server.
Install a gem:
gem install rails
16. Error Handling
Use begin/rescue for exceptions:
begin
puts 10 / 0
rescue ZeroDivisionError => e
puts "Error: #{e.message}" # => "Error: divided by 0"
end
17. Open Classes
Modify existing classes (use carefully!):
class String
def reverse_and_upcase
self.reverse.upcase
end
end
puts "hello".reverse_and_upcase # => "OLLEH"
18. Reflection
Inspect objects at runtime:
class Dog
def bark
puts "Woof!"
end
end
dog = Dog.new
puts dog.respond_to?(:bark) # => true
puts Dog.instance_methods # List all methods
Ruby’s design philosophy emphasizes developer productivity and joy. These features make it ideal for rapid prototyping, web development (with Rails), scripting, and more.
Enjoy Ruby! 🚀