Skip to content

Instantly share code, notes, and snippets.

@svarks
Created September 4, 2013 23:44
Show Gist options
  • Save svarks/6444289 to your computer and use it in GitHub Desktop.
Save svarks/6444289 to your computer and use it in GitHub Desktop.
def apply_filters!(hash_or_array)
case hash_or_array
when Array
for i in 0 ... hash_or_array.size do
apply_filters!(hash_or_array[i])
end
when Hash
hash_or_array.each do |key, value|
next if value.nil?
if value.is_a?(Hash) || value.is_a?(Array)
apply_filters!(value)
else
filters.each do |placeholder, pattern|
if pattern && key.match(pattern)
# require 'pry'
# binding.pry
hash_or_array[key] = "<#{placeholder}>"
end
end
end
end
end
@bmarini
Copy link

bmarini commented Sep 5, 2013

def apply_filters(subject, value=nil)
  case subject
  when nil
  when Array
    subject.map { |hash| apply_filters(hash) }
  when Hash
    Hash[ subject.map { |key, val| [ key, apply_filters(key, val) ] } ]
  else
    default = -> { [ value, nil ] }
    filter  = filters.find(default) { |placeholder, pattern| pattern && key.match pattern }
    "<%s>" % filter[0]
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment