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' # never gets called
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' # matches!
end

Plans

Draw inspirations from other functional languages with strong pattern matching like Haskell, Elixir, Scala, and Swift. Ideas:

  1. Any (Object is not truly Any, BasicObject is closer - e.g. Delegator)
  2. nil (just a convenience for NilClass)
  3. Closure arguments
  4. 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
# Add a + method to procs so more patterns can be stacked to the definition
play_with_string += match_proc(String, Integer) do |string, number = 2|
  string * number
end
# This won't work on normal procs
proc {} + proc {} # raises error

# Call site
play_with_string.('Hello ', 3) # 'Hello Hello Hello '
play_with_string.('Hello ', 'World') # 'Hello World'

# With keyword arguments
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

# Call site
run_with_options.(string: 'o', times: 5) # ooooo
run_with_options.(string1: 'he', string2: 'llo') # hello
run_with_options.(string1: 'Hi') # raise ArgumentError: missing keyword: string2

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

# Call site
play_with_string('Hello ', 3) # 'Hello Hello Hello '
play_with_string('Hello ', 'World') # 'Hello World'

# With keyword arguments
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

# Call site
run_with_options(start: 'He', repeat: 'l', ending: 'o') # Hello
run_with_options(start: 'He', ending: 'o') # raise ArgumentError: missing keyword: repeat
run_with_options('He', repeat: 'l', times: 2, ending: ['o', ' ', 'world']) # Hello
run_with_options('Hick') # raise ArgumentError: missing keyword: ending

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.