Skip to content

Instantly share code, notes, and snippets.

@ueno1969
Created June 24, 2016 06:42
Show Gist options
  • Save ueno1969/645528870bb8d92ecd65567fe70958dc to your computer and use it in GitHub Desktop.
Save ueno1969/645528870bb8d92ecd65567fe70958dc to your computer and use it in GitHub Desktop.
Ruby の case でワイルドカードのようなのが欲しい ref: http://qiita.com/Ueno1969/items/3e0e5dbd8a464ace369a
_ = Object.new
def _.==(obj)
true
end
[ ["a", 1], ["a", 2], ["b", 1], ["b", 2], ["c", 1], ["c", 3] ].each do |v|
pattern = case v
when ["a", 1 ] then "a,1"
when ["a", _ ] then "a,*"
when [_ , 1 ] then "*.1"
when ["c", 1 ] then "c.1" # not match
when [_ , _ ] then "*.*"
else "Not reach becase of [Any, Any] match all"
end
puts "#{v} => #{pattern}"
end
["a", 1] => a,1
["a", 2] => a,*
["b", 1] => *.1
["b", 2] => *.*
["c", 1] => *.1
["c", 3] => *.*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment