[LRUG] Feedback on Hash#dig_and_collect
Mark Burns
markthedeveloper at gmail.com
Sat Oct 21 23:40:36 PDT 2017
I think the approach of including a module in Hash is really nice when you
get started with a small project.
It allows writing concise code and keeps you focused on the core problem.
But as project sizes increase, having gems that alter core classes becomes
less desirable.
New team members still have to read about and understand the new method
first.
Occasionally adding modules or monkey patching core classes results in
obscure bugs.
E.g. You could imagine a scenario where another developer adds the same
method, and load order would determine which method is executed.
So as a general principle it's nice to make monkey patching optional.
Something like:
require "dig_and_collect/core_extension" or
require "dig_and_collect/monkey_patch" would be nice.
Then it can be an opt in for those that want it.
Also to make it useful for those without the monkey patching it would need
to be invokable via a method call from outside of a Hash.
E.g.
DigAndCollect.call(hash, *args)
On Sat, 21 Oct 2017 at 09:06, Alfredo Motta <motta.lrd at hotmail.it> wrote:
> Hello Rubyist, I am new here and looking forward to talk Ruby!
>
> I wrote this gem a while ago to make my job of collecting data from deeply
> nested hashes easier (i.e. the kind of hashes you might get back from the
> savon gem for example because they represent some underlying XML)
>
> Do you have any feedback on it?
> https://github.com/mottalrd/hash_dig_and_collect
>
> I wrote about the rationale here
>
> http://www.alfredo.motta.name/making-ruby-hashdig-even-more-awesome-introducing-hashdig_and_collect/
>
> Here is an example where I want the latitude of all the addresses of a
> given client.
> Thank you for your help,
> Happy weekend
>
> client_with_many_addresses = {
> details: {
> first_name: "Florentino",
> last_name: "Perez"
> },
> addresses: [
> {
> type: "home",
> postcode: "SE1 9SG",
> street: "London Bridge St",
> number: 32,
> city: "London",
> location: {
> latitude: 51.504382,
> longitude: -0.086279
> }
> },
> {
> type: "office",
> postcode: "SW1A 1AA",
> street: "Buckingham Palace Road",
> number: nil,
> city: "London",
> location: {
> latitude: 51.5013673,
> longitude: -0.1440787
> }
> }
> ]
> }
>
> client_with_many_addresses.dig_and_collect(:addresses, :location, :latitude)
> #=> [51.504382, 51.5013673]
>
> --
> *Alfredo Motta*, Ph.D. Software Engineering
> About me: https://www.alfredo.motta.name
> Linkedin: https://linkedin.com/in/alfredomotta
>
> _______________________________________________
> Chat mailing list
> Chat at lists.lrug.org
> Archives: http://lists.lrug.org/pipermail/chat-lrug.org
> Manage your subscription: http://lists.lrug.org/options.cgi/chat-lrug.org
> List info: http://lists.lrug.org/listinfo.cgi/chat-lrug.org
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lrug.org/pipermail/chat-lrug.org/attachments/20171022/d9c19f44/attachment-0004.html>
More information about the Chat
mailing list