Created
January 10, 2019 07:42
-
-
Save oshanz/7bb28da389f4f88b1cab4911b91f80f6 to your computer and use it in GitHub Desktop.
functional ruby
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
users_url = URI('https://jsonplaceholder.typicode.com/users') | |
calls = -> method_name, *args { | |
-> object { | |
object.public_send(method_name, *args) | |
} | |
} | |
gets_value = -> key_name { calls[:[], key_name] } | |
email_ends_with = -> what { | |
gets_value['email'] >> calls[:end_with?, what] | |
} | |
extracts = -> *keys { | |
-> object { object.slice(*keys) } | |
} | |
Net::HTTP.get(users_url) | |
.then(&JSON.method(:parse)) | |
.select(&email_ends_with['biz']) | |
.map(&extracts['name', 'email']) | |
=> [{"name"=>"Leanne Graham", "email"=>"Sincere@april.biz"}, | |
{"name"=>"Kurtis Weissnat", "email"=>"Telly.Hoeger@billy.biz"}, | |
{"name"=>"Clementina DuBuque", "email"=>"Rey.Padberg@karina.biz"}] | |
------------------- | |
https://medium.com/@baweaver/mf-abusing-rubys-operator-precedence-ccf3f071bad8 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment