Giter Site home page Giter Site logo

multi_block's Introduction

MultiBlock

MultiBlock is a mini framework for passing multiple blocks to methods. It uses named procs to accomplish this with a simple syntax. The receiving method can either yield all blocks, or just call specific ones, identified by order or name.

Currently supports CRuby only.

Setup

Add to Gemfile:

gem 'multi_block'

Usage

Defining methods that use multiple blocks

The first argument given to yield always defines the desired block(s). The other arguments get directly passed to the block(s):

yield                                            # calls all given procs without args
yield :success                                   # calls :success proc without args
yield :success, "Code Brawl!"                    # calls :success proc with message
yield 1                                          # calls first proc (:success in this case)
yield [:success, :bonus]                         # calls :success and :bonus without args
yield [:success, :bonus], "Code Brawl!"          # calls both procs with same arg
yield success: "Code Brawl!",                    # calls each keyed proc,
      error:   [500, "Internal Brawl Error"]     #       values are the args

Calling methods with multiple blocks

Consider these two example methods:

# calls the :success block if everything worked well
def ajax
  yield rand(6) != 0 ? :success : :error
end

# calls the n-th block
def dice
  random_number = rand(6)
  yield random_number, random_number + 1
end

Multiple blocks can be passed using blocks:

ajax &blocks[
  proc.success{ puts "Yeah!"    },
  proc.error  { puts "Error..." },
]

The dice method could, for example, be called in this way:

dice &blocks[
  proc{ ":(" },
  proc{ ":/" },
  proc{ ":O" },
  proc{ ":b" },
  proc{ ":P" },
  proc{ rand(42) != 0 ? ":)"  : ":D"},
]

Bonus sugar: Array extension

If you like the slim &to_proc operator, you can further optimize the syntax by activating the core extension for array:

require 'multi_block/array'

Now you do not need the blocks helper anymore. Instead just do:

do_something, some_argument, &[
  proc.easy_way{
    # do it the easy way
  },
  proc.complex_way{
    # use complex heuristics, etc.
  },
]

MIT License

See the original gist: https://gist.github.com/4b2f5fd0b45118e46d0f

multi_block's People

Contributors

janlelis avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.