{"id":17681718,"url":"https://github.com/jgaskins/method_pattern","last_synced_at":"2026-03-03T21:02:37.343Z","repository":{"id":56883456,"uuid":"126675447","full_name":"jgaskins/method_pattern","owner":"jgaskins","description":"Pattern matching for Ruby methods","archived":false,"fork":false,"pushed_at":"2018-06-09T06:28:45.000Z","size":11,"stargazers_count":20,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-12-04T11:54:10.480Z","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/jgaskins.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-03-25T07:44:49.000Z","updated_at":"2022-12-13T19:06:50.000Z","dependencies_parsed_at":"2022-08-20T13:10:40.858Z","dependency_job_id":null,"html_url":"https://github.com/jgaskins/method_pattern","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/jgaskins/method_pattern","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jgaskins%2Fmethod_pattern","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jgaskins%2Fmethod_pattern/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jgaskins%2Fmethod_pattern/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jgaskins%2Fmethod_pattern/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jgaskins","download_url":"https://codeload.github.com/jgaskins/method_pattern/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jgaskins%2Fmethod_pattern/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29970545,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-01T13:32:00.443Z","status":"ssl_error","status_checked_at":"2026-03-01T13:32:00.084Z","response_time":124,"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":[],"created_at":"2024-10-24T09:11:59.285Z","updated_at":"2026-03-03T21:02:37.330Z","avatar_url":"https://github.com/jgaskins.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MethodPattern\n\nPattern matching for Ruby methods\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'method_pattern'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install method_pattern\n\n## Usage\n\nExtend any class you want to be able to use pattern matching with the `MethodPattern` module:\n\n```ruby\nclass HandleResponse\n  extend MethodPattern\nend\n```\n\nThen you can define your method with `defn`:\n\n```ruby\nclass HandleResponse\n  extend MethodPattern\n\n  defn :call do\n    # ...\n  end\nend\n```\n\nInside of your `defn` block, you declare your argument patterns using `with`:\n\n```ruby\nclass Fibonnaci\n  extend MethodPattern\n\n  defn :call do\n    with(0..1) { |n| n }\n    with(Integer) { |n| call(n - 1) + call(n - 2) }\n  end\nend\n```\n\nThis example will handle 0 and 1 as special cases and all other integers are funneled into the second implementation. Patterns declared higher take precedence.\n\nNotice that we could pass in a class or even a range for our pattern. There are several things we can use:\n\n- Strings: `with('hello') { |str| ... }` matches an exact string\n- Numbers: `with(15) { |num| ... }` matches an exact number\n- Symbol: `with(:foo) { |sym| ... }` matches a particular symbol\n- Class: `with(Integer) { |num| ... }` matches any instance of the given class\n- Regex: `with(/foo/) { |str| ... }` matches any string that matches the regex\n- Range: `with(0...10) { |num| ... }` matches any value covered by the range\n- Proc/lambda: `with(-\u003e n { n \u003e 3 }) { |n| ... }` matches if the proc returns a truthy value\n\nNote that the method arguments are passed to the block. This lets the block become the method body.\n\n### It's not just for single arguments\n\nYou can pass multiple patterns to `with` and it will match them in order:\n\n```ruby\ndefn :baz do\n  with('foo', /bar/) { |a, b| a + b }\nend\n```\n\n### Keyword arguments\n\nKeyword arguments work, too:\n\n```ruby\nclass HandleResponse\n  extend MethodPattern\n\n  defn :call do\n    with status: 200...300, headers: { 'Content-Type': /json/ } do |body:, **|\n      JSON.parse(body, symbolize_names: true)\n    end\n\n    # All 4xx and 5xx responses are errors\n    with(status: 400..599) { |body:, **| ErrorResponse.call body }\n  end\nend\n```\n\n### Caveats\n\nUnfortunately, because `with` accepts its own block, you cannot match on whether a block was passed to the method.\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.\n\nTo install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/jgaskins/method_pattern. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n\n## Code of Conduct\n\nEveryone interacting in the MethodPattern project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/jgaskins/method_pattern/blob/master/CODE_OF_CONDUCT.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjgaskins%2Fmethod_pattern","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjgaskins%2Fmethod_pattern","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjgaskins%2Fmethod_pattern/lists"}