{"id":15561059,"url":"https://github.com/janlelis/multi_block","last_synced_at":"2025-04-23T21:48:30.726Z","repository":{"id":29016277,"uuid":"32543480","full_name":"janlelis/multi_block","owner":"janlelis","description":"Pass multiple blocks to a Ruby method","archived":false,"fork":false,"pushed_at":"2021-12-29T20:53:09.000Z","size":20,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-05-08T18:19:40.850Z","etag":null,"topics":["blocks","lambda","ruby","syntax"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/janlelis.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"MIT-LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-03-19T20:04:13.000Z","updated_at":"2022-02-22T22:33:17.000Z","dependencies_parsed_at":"2022-08-21T00:20:19.303Z","dependency_job_id":null,"html_url":"https://github.com/janlelis/multi_block","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/janlelis%2Fmulti_block","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/janlelis%2Fmulti_block/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/janlelis%2Fmulti_block/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/janlelis%2Fmulti_block/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/janlelis","download_url":"https://codeload.github.com/janlelis/multi_block/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250522296,"owners_count":21444510,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["blocks","lambda","ruby","syntax"],"created_at":"2024-10-02T16:05:07.339Z","updated_at":"2025-04-23T21:48:30.711Z","avatar_url":"https://github.com/janlelis.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MultiBlock [\u003cimg src=\"https://badge.fury.io/rb/multi_block.svg\" /\u003e](https://badge.fury.io/rb/multi_block) [\u003cimg src=\"https://github.com/janlelis/multi_block/workflows/Test/badge.svg\" /\u003e](https://github.com/janlelis/multi_block/actions?query=workflow%3ATest)\n\nMultiBlock is a mini framework for passing multiple blocks to methods. It uses [named procs](https://github.com/janlelis/named_proc) 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.\n\nCurrently supports CRuby only.\n\n## Setup\n\nAdd to Gemfile:\n\n    gem 'multi_block'\n\n\n## Usage\n### Defining methods that use multiple blocks\n\nThe first argument given to yield always defines the desired block(s). The other arguments get directly passed to the block(s):\n\n    yield                                            # calls all given procs without args\n    yield :success                                   # calls :success proc without args\n    yield :success, \"Code Brawl!\"                    # calls :success proc with message\n    yield 1                                          # calls first proc (:success in this case)\n    yield [:success, :bonus]                         # calls :success and :bonus without args\n    yield [:success, :bonus], \"Code Brawl!\"          # calls both procs with same arg\n    yield success: \"Code Brawl!\",                    # calls each keyed proc,\n          error:   [500, \"Internal Brawl Error\"]     #       values are the args\n\n\n### Calling methods with multiple blocks\n\nConsider these two example methods:\n\n    # calls the :success block if everything worked well\n    def ajax\n      yield rand(6) != 0 ? :success : :error\n    end\n\n    # calls the n-th block\n    def dice\n      random_number = rand(6)\n      yield random_number, random_number + 1\n    end\n\n\nMultiple blocks can be passed using `blocks`:\n\n    ajax \u0026blocks[\n      proc.success{ puts \"Yeah!\"    },\n      proc.error  { puts \"Error...\" },\n    ]\n\n\nThe dice method could, for example, be called in this way:\n\n    dice \u0026blocks[\n      proc{ \":(\" },\n      proc{ \":/\" },\n      proc{ \":O\" },\n      proc{ \":b\" },\n      proc{ \":P\" },\n      proc{ rand(42) != 0 ? \":)\"  : \":D\"},\n    ]\n\n\n## Bonus sugar: Array extension\n\nIf you like the slim `\u0026to_proc` operator, you can further optimize the syntax by activating the core extension for array:\n\n    require 'multi_block/array'\n\nNow you do not need the `blocks` helper anymore. Instead just do:\n\n    do_something, some_argument, \u0026[\n      proc.easy_way{\n        # do it the easy way\n      },\n      proc.complex_way{\n        # use complex heuristics, etc.\n      },\n    ]\n\n\n## MIT License\n\nSee the original gist: https://gist.github.com/4b2f5fd0b45118e46d0f\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjanlelis%2Fmulti_block","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjanlelis%2Fmulti_block","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjanlelis%2Fmulti_block/lists"}