{"id":16239735,"url":"https://github.com/jalkoby/multidispatch_dsl","last_synced_at":"2025-07-19T03:37:32.745Z","repository":{"id":10887288,"uuid":"13177873","full_name":"jalkoby/multidispatch_dsl","owner":"jalkoby","description":"Add mutlti dispatch functionality to Ruby","archived":false,"fork":false,"pushed_at":"2013-10-03T08:52:06.000Z","size":128,"stargazers_count":10,"open_issues_count":1,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-03T04:41:37.699Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/jalkoby.png","metadata":{"files":{"readme":"README.rdoc","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-09-28T17:41:32.000Z","updated_at":"2019-07-03T09:29:19.000Z","dependencies_parsed_at":"2022-08-29T15:10:52.178Z","dependency_job_id":null,"html_url":"https://github.com/jalkoby/multidispatch_dsl","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/jalkoby/multidispatch_dsl","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jalkoby%2Fmultidispatch_dsl","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jalkoby%2Fmultidispatch_dsl/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jalkoby%2Fmultidispatch_dsl/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jalkoby%2Fmultidispatch_dsl/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jalkoby","download_url":"https://codeload.github.com/jalkoby/multidispatch_dsl/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jalkoby%2Fmultidispatch_dsl/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265883680,"owners_count":23843800,"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":[],"created_at":"2024-10-10T13:44:56.417Z","updated_at":"2025-07-19T03:37:32.720Z","avatar_url":"https://github.com/jalkoby.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"= MultidispatchDSL\n\nIf you worked with other languages you probably know multi dispatch feature - define variants of same method with\ndifferent number of arguments of different types. In Ruby with it's duck typing you can't do it from out of box. This\ngem will allow you to do it.\n\n{\u003cimg src=\"https://codeclimate.com/github/jalkoby/multidispatch_dsl.png\" /\u003e}[https://codeclimate.com/github/jalkoby/multidispatch_dsl]\n{\u003cimg src=\"https://travis-ci.org/jalkoby/multidispatch_dsl.png?branch=master\" alt=\"Build Status\" /\u003e}[https://travis-ci.org/jalkoby/multidispatch_dsl]\n{\u003cimg src=\"https://badge.fury.io/rb/multidispatch_dsl.png\" alt=\"Gem Version\" /\u003e}[http://badge.fury.io/rb/multidispatch_dsl]\n\n== Installation\n\nAdd this line to your application's Gemfile:\n\n    gem 'multidispatch_dsl'\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install multidispatch_dsl\n\n== Usage\n\nJust include MultidispatchDSL into your class and your will get `mdef` class method. For example\n\n  class TestClass\n    include MultidispatchDSL\n\n    # hello method with one integer argument\n    mdef(:hello, Fixnum) do |i|\n      \"Fixnum version with number #{ i }\"\n    end\n\n    # hello method with one string argument\n    mdef(:hello, String) do |str|\n      \"String version with string #{ str }\"\n    end\n\n    # hello method with fixnum and string arguments\n    mdef(:hello, Fixnum, String) do |i, str|\n      \"Fixnum String version with #{ i } \u0026 #{ str }\"\n    end\n\n    # hello method with string and fixnum arguments\n    mdef(:hello, String, Fixnum) do |str, i|\n      \"String Fixnum version with #{ str } \u0026 #{ i }\"\n    end\n\n    # hello method without arguments\n    mdef(:hello) do\n      \"Version without args\"\n    end\n\n    # hello method with symbol argument\n    mdef(:hello, Symbol) do |symbol|\n      \"Symbol version with :#{ symbol } \u0026 :#{ internal_method }\"\n    end\n\n    # hello method with 2 string argument and nested block\n    mdef(:hello, String, String) do |str_one, str_two, \u0026block|\n      instance_exec(str_one.upcase, str_two.downcase, \u0026block)\n    end\n\n    def internal_method\n      :internal_method\n    end\n  end\n\n  test_instance = TestClass.new\n\n  test_instance.hello\n  #=\u003e \"Version without args\"\n\n  test_instance.hello(1)\n  #=\u003e \"Fixnum version with number 1\"\n\n  test_instance.hello(:foo)\n  #=\u003e \"Symbol version with :foo \u0026 :internal_method\"\n\n  test_instance.hello(1, \"string\")\n  #=\u003e \"Fixnum String version with 1 \u0026 string\"\n\n  test_instance.hello(\"string\", 1)\n  #=\u003e \"String Fixnum version with string \u0026 1\"\n\n  test_instance.hello(:not, :defined)\n  #=\u003e raise error MultidispatchDSL::MissingDeclarationError\n\n  test_instance.hello('One', 'Two') { |str_one, str_two| \"#{str_one} #{ str_two } #{ internal_method }\" }\n  #=\u003e \"ONE two internal_method\"\n\nAs you can see defining version of method with *yield* is little bit tricky. That because defining method from block add extra scope. More details about it here http://www.andylindeman.com/2011/01/08/defining-methods-using-blocks-in-ruby.html\n\nIf you need define version of method for any number arguments of any types use `:anything` symbol\n\n  mdef(:process, String) do |value|\n    { :string =\u003e value }\n  end\n\n  mdef(:process, Fixnum) do |value|\n    { :int =\u003e value }\n  end\n\n  mdef(:process, :anything) do |value|\n    { :anything =\u003e value }\n  end\n\nIf you desire add this functionality to all classes just include MultidispatchDSL to Object class:\n\n  Object.send(:include, MultidispatchDSL)\n\n== Requirements\n\n  Ruby \u003e= 1.9.2 and other ruby implementations with 1.9 mode\n\n== Contributing\n\n1. Fork it\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Add some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create new Pull Request\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjalkoby%2Fmultidispatch_dsl","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjalkoby%2Fmultidispatch_dsl","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjalkoby%2Fmultidispatch_dsl/lists"}