Skip to content

Instantly share code, notes, and snippets.

@adrdra
Last active April 24, 2018 10:32
Show Gist options
  • Select an option

  • Save adrdra/81b78b8f3746b233f1b2a6a778b7c253 to your computer and use it in GitHub Desktop.

Select an option

Save adrdra/81b78b8f3746b233f1b2a6a778b7c253 to your computer and use it in GitHub Desktop.
Formatter
arr = [{ lol: 'lol', mdr: 'mdr' }]
def transform_array(list, formatters = {})
return list if formatters.empty?
list.map { |item|
formatters.reduce(item.dup) { |dup, (key, formatter)|
dup[key] = formatter === item.public_send(:[], key)
dup
}
}
end
transform_array(arr, { lol: -> a { a.upcase } })
# => [{:lol=>"LOL", :mdr=>"mdr"}]
p arr
# => [{ lol: 'lol', mdr: 'mdr' }]
:camelize.to_proc
# => -> x { x.camelize }
['lol'].map(&:camelize) == ['lol'].map(&(:camelize.to_proc))
# => true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment