Skip to content
Blox helps you write Ruby methods that accept multiple blocks
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore
Gemfile
LICENSE
README.md
Rakefile
blox.gemspec
blox.rb

README.md

Blox

A tiny Ruby gem to aid you when you wish to pass multiple blocks to any ruby method.

Examples

Passing multiple blocks to a method

def accepts_multiple_blocks(is_awesome, &block)
  if is_awesome
    block.yield_to(:awesome, "this is awesome!") do
      puts "You may pass blocks to the blocks, too!"
    end
  else
    block.yield_to(:not_awesome, "bad, this is bad")
  end
end

accepts_multiple_blocks(true) do |is|
  is.awesome do |&block|
    puts "Very awesome!"
    block.call
  end

  is.not_awesome do
    puts "Not so awesome"
  end
end

accepts_multiple_blocks(false) do |event|
  if event.not_awesome?
    puts ":("
  else
    puts "\\o/!"
  end
end

If you wish to enforce that at least one of the blocks are executed, you may use block.yield\_to! (with a bang) instead, which will raise an exception if no block was executed.

License

2-clause (simplified) BSD license. See LICENSE for details.

You can’t perform that action at this time.