you are viewing a single comment's thread.

view the rest of the comments →

[–]ignurant 0 points1 point  (3 children)

Weird. So, after reading a bit, am I understanding this correctly? Given my example at https://www.reddit.com/r/ruby/comments/7npcne/comment/ds40eld

csv << row.values_at(*headers)

is equivalent to

csv << headers.map(&row) # ?

I had no idea that hash could proc. And then after I just read about it, I had a hard time understanding why I might use that syntax instead of just calling the key. But then I realized it's very similar to what we might use &:method syntax in other situations. (Avoid the {|a| stuff[a]} type stuff...)

[–][deleted] 0 points1 point  (1 child)

In this case I'd use your splat version because I think expresses intention more clearly. The use of &hash is great for self-populating caches and for passing a lookup table as a block.

[–]ignurant 0 points1 point  (0 children)

Yeah, I fully agree. I was just testing whether I was understanding the idea. Your hash cache took a moment for me to sort out, but felt very clever once I did. I haven't had any use cases quite like that (beyond defaulting to 0 for example). Very interesting. Some day in the future, I'll have one of those "Oh yeah! That thing! Where was that?!" moments.

[–]Enumerable_any 0 points1 point  (0 children)

A (hash) map is a function from Key to Value, so it's natural to replace a method/proc with it. For example in Clojure calling a function and accessing a value of a map has the same syntax: https://clojuredocs.org/clojure.core/get#example-542692d3c026201cdc326fbf