Created
April 19, 2019 08:11
-
-
Save kokuyouwind/63a0d0cd7f7e83ced63aab098a430763 to your computer and use it in GitHub Desktop.
rubyのpattern matchの実験
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class None | |
def to_s | |
'None' | |
end | |
end | |
class Some | |
def initialize(v) | |
@v = v | |
end | |
def to_s | |
"Some(#{@v})" | |
end | |
def deconstruct | |
[@v] | |
end | |
end | |
def double_opt(o) | |
case o | |
in None | |
o | |
in Some(v) | |
Some.new(v * 2) | |
end | |
end | |
puts None.new | |
puts double_opt(None.new) | |
puts Some.new(2) | |
puts double_opt(Some.new(2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment