Last active
April 24, 2018 10:32
-
-
Save adrdra/81b78b8f3746b233f1b2a6a778b7c253 to your computer and use it in GitHub Desktop.
Formatter
This file contains hidden or 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
| 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' }] |
This file contains hidden or 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
| :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