Ruby is known for its flexibility and simplicity, allowing developers to work with different programming paradigms, such as procedural, object-oriented, and functional. Each of these styles has its strengths and can be applied in Ruby on Rails projects to solve specific problems elegantly. In this post, we’ll explore each paradigm with practical examples.
Procedural Paradigm
The procedural paradigm organizes code as a sequence of instructions to be executed in order. Although Ruby is predominantly object-oriented, this style can still be applied in specific parts of a Rails application, such as initialization scripts or Rake tasks.
Example: Data Import Script
A common example of procedural programming in Rails is creating a script to import data into the database:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# lib/tasks/import_data.rake
namespace :data do
desc "Imports data from a CSV file into the Users table"
task import_users: :environment do
require 'csv'
file_path = Rails.root.join('data', 'users.csv')
CSV.foreach(file_path, headers: true) do |row|
User.create!(row.to_hash)
end
puts "Import completed!"
end
end
In this example, the code follows a linear sequence of steps to read a CSV file and insert data into the Users
table.
Object-Oriented Paradigm
In the object-oriented paradigm, code is structured into classes and objects that encapsulate data and behavior. This is the primary paradigm used in Ruby on Rails.
Example: Model with Encapsulated Methods
Rails models are a clear example of object-oriented programming. They encapsulate business logic and interact with the database.
1
2
3
4
5
6
7
8
9
10
11
12
13
# app/models/order.rb
class Order < ApplicationRecord
has_many :order_items
belongs_to :user
def total_price
order_items.sum { |item| item.price * item.quantity }
end
def mark_as_completed
update!(status: 'completed', completed_at: Time.current)
end
end
In this example, the Order
class encapsulates data (attributes from the database) and behavior (total_price
and mark_as_completed
), promoting reusability and modularity.
Functional Paradigm
The functional paradigm prioritizes immutability and pure functions, avoiding state changes and side effects. In Ruby, this style can be applied using methods like map
, reduce
, and lambdas.
Example: Functional Data Processing
In a Rails controller, we can apply the functional paradigm to process data declaratively:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# app/controllers/reports_controller.rb
class ReportsController < ApplicationController
def index
@report = generate_report
end
private
def generate_report
Order.completed.map do |order|
{
user: order.user.name,
total_price: order.total_price,
completed_at: order.completed_at
}
end
end
end
In this example, the generate_report
method uses map
to transform a collection of completed orders (Order.completed
) into a formatted report without modifying the original objects.