Learn Ruby
Learn Ruby
This guide helps you write business rules in Ruby. Ruby is a readable, expressive language — these resources will get you started.
Interactive tutorials
- Try Ruby in your browser — 15-minute interactive tutorial, no setup needed
- Ruby in 20 Minutes — official quickstart guide
Reference docs
- Ruby-Doc.org — full Ruby core and standard library docs
- Official Ruby docs
- DevDocs Ruby — fast, offline-capable docs
Common patterns for rules
Working with strings
record.name = record.name.strip
record.name = record.name.upcase
record.name = record.name.downcase
record.name = record.name.capitalize
record.name = "prefix_#{record.name}"
Working with dates and times
now = Time.current
today = Date.current
record.due_date = 7.days.from_now
record.completed_at = Time.current if record.status == "done"
Checking conditions
if record.name.blank?
raise Rules::RuleError, "Name is required"
end
if record.email.present? && record.email.include?("@")
record.domain = record.email.split("@").last
end
Fetching external data
require "net/http"
uri = URI("https://example.com")
resp = Net::HTTP.get_response(uri)
record.http_status = resp.code
Creating audit logs
Log.create!(
source: "rule",
message: "Updated record #{record.id}",
context: { actor: actor&.username }.to_json
)
Tips
- Each rule runs in its own scope — local variables don't leak
- Use
raise Rules::RuleErrorto abort the action with a message - In
before_*stages, modifying the record changes what gets saved - In
after_*stages, the record is already saved — use side effects - Check
actorto know who triggered the action - Use
attributesto see what form values were submitted
Back: