Ruby Logo

Functional Programming Patterns, Callbacks & DSL Creation

Master Ruby's functional programming capabilities, advanced callback patterns, and DSL creation techniques for elegant, maintainable code.

Home Ruby Functional Programming Patterns, Callbacks & DSL Creation

Functional Programming Mastery

Master functional programming patterns in Ruby. Learn how to use callbacks, create domain-specific languages (DSLs), and implement function composition for more expressive and maintainable code.

Callbacks

Callbacks are functions passed as arguments to other functions, executed at specific points in the program flow.

# Event system with callbacks
class EventEmitter
def initialize
@listeners = Hash.new { |h, k| h[k] = [] }
end
def on(event, &callback)
@listeners[event] << callback
end
def emit(event, data = nil)
@listeners[event].each { |callback| callback.call(data) }
end
end
emitter = EventEmitter.new
emitter.on(:user_login) { |user| puts \"User #{user} logged in\" }
emitter.on(:user_logout) { |user| puts \"User #{user} logged out\" }
emitter.emit(:user_login, \"Alice\")
emitter.emit(:user_logout, \"Alice\")

Domain-Specific Languages (DSLs)

Create expressive, domain-specific languages using Ruby's flexible syntax and metaprogramming capabilities.

# Configuration DSL
class ConfigBuilder
def initialize
@config = {}
end
def database(&block)
@config[:database] = DatabaseConfig.new
@config[:database].instance_eval(&block)
end
def to_h
@config
end
end
class DatabaseConfig
def host(value); @host = value; end
def port(value); @port = value; end
def name(value); @name = value; end
end
config = ConfigBuilder.new
config.database do
host \"localhost\"
port 5432
name \"myapp\"
end

Function Composition

Combine simple functions to create more complex behavior through composition.

# Function composition utilities
class FunctionComposer
def self.compose(*functions)
->(x) { functions.reduce(x) { |acc, f| f.call(acc) } }
end
def self.pipe(*functions)
compose(*functions)
end
end
# Simple functions
add_one = ->(x) { x + 1 }
multiply_by_two = ->(x) { x * 2 }
square = ->(x) { x * x }
# Compose functions
composed = FunctionComposer.compose(add_one, multiply_by_two, square)
puts composed.call(3) # => 64 ((3+1)*2)^2

Interactive Code Runner

Practice Time: Try these functional programming examples and experiment with different patterns.

Interactive Code Runner

Ruby Code Editor
Output will appear here when you run the code...

Best Practices

1. Keep Functions Pure

Pure functions with no side effects are easier to test, reason about, and compose.

2. Use Composition Over Inheritance

Compose simple functions to create complex behavior rather than building deep inheritance hierarchies.

3. Design Clear DSLs

Make your DSLs read like natural language and provide clear error messages.

Summary

Functional programming in Ruby enables:

  • Event-driven programming with callbacks
  • Expressive domain-specific languages
  • Function composition for complex behavior
  • Higher-order functions for abstraction
  • More testable and maintainable code

Use functional programming patterns to write more expressive, flexible, and maintainable Ruby code.

Procs & Lambdas
#

Quick Navigation

Read Topic
Watch Video Tutorial

Related Topics

Blocks → Methods → Classes →

Back to Ruby Home

Video Tutorial

Watch and learn functional programming patterns, callbacks & dsl creation

Pro Tip: After reading through the content above, watch this video to reinforce your understanding and see the concepts in action!