Skip to content

Instantly share code, notes, and snippets.

@kokuyouwind
Created April 19, 2019 08:11
Show Gist options
  • Save kokuyouwind/63a0d0cd7f7e83ced63aab098a430763 to your computer and use it in GitHub Desktop.
Save kokuyouwind/63a0d0cd7f7e83ced63aab098a430763 to your computer and use it in GitHub Desktop.
rubyのpattern matchの実験
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