PatternMatchable
call method with pattern match.
Installation
Add this line to your application's Gemfile:
gem 'pattern_matchable'
And then execute:
$ bundle install
Or install it yourself as:
$ gem install pattern_matchable
Usage
require "pattern_matchable"
using PatternMatchable Array
case [1, 2, 3]
in first:, last:
pp "OK : #{first}, #{last}"
end
1. using PatternMatchable {class names}
Use Refinements to add {class names}#deconstruct_keys
.
require "pattern_matchable"
using PatternMatchable Array
[1, 2, 3, 4, 5] => { first:, last: }
p first
p last
"Homu" in { downcase:, upcase: }
2. using PatternMatchable
Use Refinements to add Object#deconstruct_keys
.
require "pattern_matchable"
using PatternMatchable
case [1, 2, 3, 4, 5]
in { first:, last: }
end
p first
p last
case "Homu"
in { downcase:, upcase: }
end
p downcase
p upcase
3. include PatternMatchable
include
and add #deconstruct_keys
to any class / module.
require "pattern_matchable"
class Array
include PatternMatchable
end
case [1, 2, 3, 4, 5]
in { first:, last: }
end
p first
p last
case "Homu"
in { downcase:, upcase: }
end
4. using PatternMatchable.refining klass
Add #deconstruct_keys
to any class / module using Refinements.
require "pattern_matchable"
using PatternMatchable.refining Array
[1, 2, 3, 4, 5] => { first:, last: }
p first
p last
case "Homu"
in { downcase:, upcase: }
end
5. using PatternMatchable::#{class name}
Use Refinements
using const_missing
.
require "pattern_matchable"
using PatternMatchable::Array
case [1, 2, 3, 4, 5]
in { first:, last: }
end
p first
p last
using PatternMatchable::Enumerator::Lazy
(1..10).lazy.map { _1 * 2 } => { first:, count: }
p first
p count
"Homu" in { downcase:, upcase: }
NOTE: Classes with #deconstruct_keys
or #respond_to?
defined will not work with using PatternMatchable
.
require "pattern_matchable"
class X
def respond_to?(...)
super
end
end
using PatternMatchable
case { name: "homu", age: 14 }
in { first:, count: }
else
end
case X.new
in { __id__: }
else
end
For example, ActiveRecord::Base
.
In this case, you must use using PatternMatchable X
.
require "pattern_matchable"
class X
def respond_to?(...)
super
end
end
using PatternMatchable Hash
using PatternMatchable X
case { name: "homu", age: 14 }
in { first:, count: }
pp "ok: #{first}: #{count}"
else
end
case X.new
in { __id__: }
pp "ok: #{__id__}"
else
end
Development
After 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.
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/osyo-manga/pattern_matchable.