Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding scala-style matching #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

camertron
Copy link

This PR adds scala-style matching to options so you can do stuff like this:

result = Option(variable).match do |matcher|
  matcher.case(Some[Fixnum]) { |val| val * 2 }
  matcher.case(Some[String]) { |val| val.length }
end

result  # => 10 when variable is 5, 6, when variable is "option"

You can also match without types:

Option(variable).match do |matcher|
  matcher.case(Some) { |val| puts val }
  matcher.case(None) { |val| raise StandardError }
end

Finally, the OptionMatcher also supports else:

Option(variable).match do |matcher|
  matcher.case(Some[String]) { ... }
  matcher.else { |val| ... }
end

@rares
Copy link
Owner

rares commented Mar 11, 2014

ack

I'm a little swamped right now so I will probably take me a few weeks to review.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants