Skip to content

Instantly share code, notes, and snippets.

@oshanz
Created January 10, 2019 07:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oshanz/7bb28da389f4f88b1cab4911b91f80f6 to your computer and use it in GitHub Desktop.
Save oshanz/7bb28da389f4f88b1cab4911b91f80f6 to your computer and use it in GitHub Desktop.
functional ruby
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