{"id":15405692,"url":"https://github.com/zverok/matchish","last_synced_at":"2025-03-21T15:43:05.493Z","repository":{"id":35116877,"uuid":"39307336","full_name":"zverok/matchish","owner":"zverok","description":"An exercise for pattern matching in Ruby","archived":false,"fork":false,"pushed_at":"2015-07-20T06:58:56.000Z","size":136,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-26T11:11:14.905Z","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":"Amorem/FlutterSocial","license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zverok.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-07-18T17:52:33.000Z","updated_at":"2018-02-19T15:30:47.000Z","dependencies_parsed_at":"2022-09-16T14:21:14.368Z","dependency_job_id":null,"html_url":"https://github.com/zverok/matchish","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zverok%2Fmatchish","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zverok%2Fmatchish/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zverok%2Fmatchish/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zverok%2Fmatchish/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zverok","download_url":"https://codeload.github.com/zverok/matchish/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244823934,"owners_count":20516376,"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-01T16:18:16.362Z","updated_at":"2025-03-21T15:43:05.458Z","avatar_url":"https://github.com/zverok.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Matchish\n\n*Matchish* is an exercise in implementing pattern-matching for Ruby. It\nis *not* a production-level code (and *not* released as a gem therefore),\nbut just a proof-of-concept.\n\nYou can look at corresponding [blog post](http://zverok.github.io/blog/2015-07-18-matchish.html)\nto see the rationale and overview, and also the rationale of why it is\nnot a gem.\n\nNevertheless, features:\n\n* As natural to Ruby as it could be:\n\n```ruby\nrequire 'matchish'\nrequire 'matchish/m' # shortening of all \"matchish\" to \"m\"\n\ncase arg\nwhen String # your usual Ruby check\nwhen Fixnum.m.guard{|x| x \u003e 3} # Matchish!\nwhen [Object.m.as(:x), *Object.m].m # Matchish!\n\nend\n\n# Or, another example:\n\n%w[one two three].grep(/tw/) # your usual Ruby grep\n%w[one two three].grep(String.m.with(length: 3)) # Matchish!\n```\n\n* Complex patterns and Ruby types being algebraic\n\n```ruby\ncase arg\nwhen [{test: String.m}.m, Object.m, *Hash.m].m\n  # catches ONLY array with:\n  # * first item being a hash with exactly one key: :test, and value - kind of string,\n  # * second item being any value\n  # * then any number of items, each of them should be hashes\nwhen Time.m.with(year: 2015)\n  # catches ONLY time with year == 2015\nend\n```\n\n* On-the-fly deconstruction\n\n```ruby\n# local variable flavor\ncase arg\nwhen [(x = Fixnum.m), x, *Object.m].m # matches [1,1,3,4], but not [1,2,3,4]\n  p x.value # =\u003e 1\nend\n\n# as/match flavor\ncase arg\nwhen [Fixnum.m.as(:x), Object.m.as(:x), *Object.m].m # matches [1,1,3,4], but not [1,2,3,4]\n  p Matchish.last_match[:x] # =\u003e 1\nend\n```\n\n* Guards\n\n```ruby\nflag = true\n\ncase x\nwhen (1..3).m.guard{flag} # only matches when flag is true\nwhen (1..3) # matches otherwise\nend\n```\n\n## Playing with code\n\n```\ngit clone git@github.com:zverok/matchish.git\nbundle install\nbundle exec dokaz Article.md\n```\n\nNB: several examples from Article.md are failing. It's ok - they are showing,\nexactly, parts of Ruby syntax which are NOT working.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzverok%2Fmatchish","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzverok%2Fmatchish","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzverok%2Fmatchish/lists"}