Skip to content

Instantly share code, notes, and snippets.

@revdan
Forked from ethagnawl/gist:dc3b2c5e64d9270928bc
Last active August 29, 2015 14:07
Show Gist options
  • Save revdan/54c6dbea14fbacf7bf8a to your computer and use it in GitHub Desktop.
Save revdan/54c6dbea14fbacf7bf8a to your computer and use it in GitHub Desktop.
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