Composing Procs

This is so simple, but yet so elegant (in my humble opinion).

def compose_procs(procs)
  lambda do |input|
    procs.inject(input) { |x, f| f[x] }
  end
end

Example: Text Filtering

filters = [
  lambda { |x| x.gsub("World", "Planet Earth") },
  lambda { |x| RedCloth.new(x).to_html }
]

compose_procs(filters)["Hello *World*!"]
# => "<p>Hello <strong>Planet Earth</strong>!</p>"