-
-
Save revdan/54c6dbea14fbacf7bf8a to your computer and use it in GitHub Desktop.
This file contains 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 Wildcards < Struct.new(:hash, :definitions) | |
def replace(a = hash, b = definitions) | |
a.merge(b) do |_, left, right| | |
if left.is_a? Hash | |
compare(left, right) | |
else | |
left == '*' ? right : left | |
end | |
end | |
end | |
# or if you want to codegolf it... | |
def replace(a = hash, b = definitions) | |
a.merge(b){|_,l,r|l.is_a?(Hash)?replace(l,r):l=='*'?r:l} | |
end | |
end | |
Wildcards.new( | |
{:foo=>"*", :bar=>{:baz=>"*", :buck=>"buck"}}, | |
{:foo=>"bar", :bar=>{:baz=>"baz", :buck=>"buck"}} | |
).replace | |
# or... | |
module WildcardParser | |
def self.replace(a, b) | |
a.merge(b){|_,l,r|l.is_a?(Hash)?replace(l,r):l=='*'?r:l} | |
end | |
end | |
WildcardParser::replace({:foo=>"*", :bar=>{:baz=>"*", :buck=>"buck"}}, {:foo=>"bar", :bar=>{:baz=>"baz", :buck=>"buck"}}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment