{"id":39824810,"url":"https://github.com/alehander92/matchete","last_synced_at":"2026-01-18T13:03:32.850Z","repository":{"id":15220797,"uuid":"17949354","full_name":"alehander92/matchete","owner":"alehander92","description":"A DSL for method overloading in Ruby based on pattern matching","archived":false,"fork":false,"pushed_at":"2015-06-09T12:49:34.000Z","size":157,"stargazers_count":55,"open_issues_count":1,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2026-01-14T11:15:00.360Z","etag":null,"topics":["dsl","gem","pattern-matching","ruby"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"robpike/ivy","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alehander92.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-03-20T16:15:43.000Z","updated_at":"2023-07-11T09:51:22.000Z","dependencies_parsed_at":"2022-09-01T22:42:05.136Z","dependency_job_id":null,"html_url":"https://github.com/alehander92/matchete","commit_stats":null,"previous_names":["alehander42/matchete"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/alehander92/matchete","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alehander92%2Fmatchete","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alehander92%2Fmatchete/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alehander92%2Fmatchete/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alehander92%2Fmatchete/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alehander92","download_url":"https://codeload.github.com/alehander92/matchete/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alehander92%2Fmatchete/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28536687,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-18T10:13:46.436Z","status":"ssl_error","status_checked_at":"2026-01-18T10:13:11.045Z","response_time":98,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["dsl","gem","pattern-matching","ruby"],"created_at":"2026-01-18T13:03:32.251Z","updated_at":"2026-01-18T13:03:32.845Z","avatar_url":"https://github.com/alehander92.png","language":"Ruby","readme":"Matchete\n=========\n\nMatchete provides a DSL for method overloading based on pattern matching for Ruby.\n\n[![Build Status](https://travis-ci.org/alehander42/matchete.svg)](https://travis-ci.org/alehander42/matchete)\n\nIt's just a quick hack inspired by weissbier and the use-return-values-of-method-definitions DSL technique used in [harmonic](https://github.com/s2gatev/harmonic)\n\n**It supports only ruby 2.1+**\n\nFeatures\n--------\n\n* `on [:value, Integer]` matches an arg with the same internal structure\n* `on '#method_name'` matches args responding to `method_name`\n* `on AClass` matches instances of `AClass`\n* `on a: 2, method:...` matches keyword args\n* `on :test?` matches with user-defined predicate methods\n* `on either('#count', Array)` matches if any of the tests returns true for an arg\n* `on full_match('#count', '#combine')` matches if all of the tests return true for an arg\n* `on exact(Integer)` matches special values, used as shortcuts in other cases:\nclasses, strings starting with '#', etc\n* `on having('#count' =\u003e 2)` matches objects with properties with certain values\n* `default` matches when no match has been found in `on` branches\n\n\n\nInstall\n-----\n`gem install matchete`\n\n\nUsage\n-----\n\n```ruby\nclass Translator\n  include Matchete\n\n  on Any, :string,\n  def translate(value, to)\n    value.to_s\n  end\n\n  on '#-@', :negative,\n  def translate(value, to)\n    - value\n  end\n\n  on String, :integer,\n  def translate(value, to)\n    value.to_i\n  end\n\n  default def translate(value, to)\n    0\n  end\nend\n\nt = Translator.new\np t.translate 72, :negative # -72\np t.translate nil, :integer # 0\n```\n\n```ruby\nrequire 'matchete'\n\nclass FactorialStrikesAgain\n  include Matchete\n\n  on 1,\n  def factorial(value)\n    1\n  end\n\n  on -\u003e x { x \u003e 1 },\n  def factorial(value)\n    value * factorial(value - 1)\n  end\nend\n\nFactorialStrikesAgain.new.factorial(4) #24\nFactorialStrikesAgain.new.factorial(-2) #Matchete::NotResolvedError No matching factorial method for args [-2]\n```\n\n```ruby\nclass Converter\n  include Matchete\n\n  on '#special_convert',\n  def convert(value)\n    value.special_convert\n  end\n\n  on Integer,\n  def convert(value)\n    [:integer, value]\n  end\n\n  on Hash,\n  def convert(values)\n    [:dict, values.map { |k, v| [convert(k), convert(v)] }]\n  end\n\n  on /reserved_/,\n  def convert(value)\n    [:reserved_symbol, value]\n  end\n\n  on String,\n  def convert(value)\n    [:string, value]\n  end\n\n  on ['deleted', [Integer, Any]],\n  def convert(value)\n    ['deleted', value[1]]\n  end\n\n  on :starts_with_cat?,\n  def convert(value)\n    [:fail, value]\n  end\n\n  on free: Integer, method:\n  def convert(free:)\n    [:rofl, free]\n  end\n\n  on either('#count', Array),\n  def convert(value)\n    value.count\n  end\n\n  on full_match('#count', '#lala'),\n  def convert(value)\n    value.count + value.lala\n  end\n\n  default def convert(value)\n    [:z, value]\n  end\n\n  def starts_with_cat?(value)\n    value.to_s.start_with?('cat')\n  end\nend\n\nclass Z\n  def special_convert\n    [:special_convert, nil]\n  end\nend\n\nconverter = Converter.new\np Converter.instance_methods\np converter.convert(2) #[:integer, 2]\np converter.convert(Z.new) #[:special_convert, nil]\np converter.convert([4, 4]) # 2\np converter.convert({2 =\u003e 4}) #[:dict, [[[:integer, 2], [:integer, 4]]]\np converter.convert('reserved_l') #[:reserved_symbol, 'reserved_l']\np converter.convert('zaza') #[:string, 'zaza']\np converter.convert(['deleted', [2, Array]]) #['deleted', [2, Array]]\np converter.convert(:cat_hehe) #[:fail, :cat_hehe]\np converter.convert(free: 2) #[:rofl, 2]\np converter.convert(2.2) #[:z, 2.2]\n\n```\n\nversion\n-------\n0.5.1\n\n\ncbb\n-----\n![](https://global3.memecdn.com/kawaii-danny-trejo_o_2031011.jpg)\n\nTodo\n-----\n* Clean up the specs, right now they're a mess.\n* Fix all kinds of edge cases\n\n\nCopyright\n-----\n\nCopyright (c) 2015 Alexander Ivanov. See LICENSE for further details.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falehander92%2Fmatchete","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falehander92%2Fmatchete","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falehander92%2Fmatchete/lists"}