Matchcase
Ruby pattern matching done easy
Installation
Add this line to your application's Gemfile:
gem 'matchcase'
And then execute:
$ bundle
Or install it yourself as:
$ gem install matchcase
Usage
Just include the gem and start pattern matching
You are used to this
case variable
when Numeric
'is number'
when String
'is string'
when Array
'is array'
end
But with more complex types...
case [1, 2]
when [Numeric, Numeric]
'array with 2 numbers'
when [1, 2]
'array containing 1 and 2'
end
What if you can do this
case [1, 2]
when [Numeric, Numeric]
'array with 2 numbers'
end
Plans
Draw inspirations from other functional languages with strong pattern matching like Haskell, Elixir, Scala, and Swift.
Ideas:
- Any (Object is not truly Any, BasicObject is closer - e.g. Delegator)
- nil (just a convenience for NilClass)
- Closure arguments
- Function arguments
Example Ideas
Any
case [1, 2]
when [Any, Integer]
'any and integer'
end
Nil
case [1, nil]
when [Integer, nil]
'same as [Integer, NilClass]'
end
Closure Arguments
play_with_string = match_proc(String, String) do |string, string|
string + string
end
play_with_string += match_proc(String, Integer) do |string, number = 2|
string * number
end
proc {} + proc {}
play_with_string.('Hello ', 3)
play_with_string.('Hello ', 'World')
run_with_options = match_proc(string: String, times: Integer) do |string:, times:|
string * times
end
run_with_options = match_proc(string1: String, string2: String) do |string1:, string2:|
string1 + string2
end
run_with_options.(string: 'o', times: 5)
run_with_options.(string1: 'he', string2: 'llo')
run_with_options.(string1: 'Hi')
Function Arguments
match_def(:play_with_string, String, String) do |string, string|
string + string
end
match_def(:play_with_string, String, Integer) do |string, number = 2|
string * number
end
play_with_string('Hello ', 3)
play_with_string('Hello ', 'World')
match_def(:run_with_options, String, repeat: String, times: Integer, ending: [String]) do |start, repeat:, times: 1, ending:|
start + repeat * times + ending.join(', ')
end
match_def(:run_with_options, start: String, repeat: String, times: Integer, ending: String) do |start: '', repeat:, times: 2, ending: ''|
start + repeat * times + ending
end
run_with_options(start: 'He', repeat: 'l', ending: 'o')
run_with_options(start: 'He', ending: 'o')
run_with_options('He', repeat: 'l', times: 2, ending: ['o', ' ', 'world'])
run_with_options('Hick')
Development
After checking out the repo, run bin/setup
to install dependencies. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To 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.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/matrinox/matchcase. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
License
The gem is available as open source under the terms of the MIT License.