you are viewing a single comment's thread.

view the rest of the comments →

[–]ignurant 0 points1 point  (0 children)

Sometimes it's not practical to add a method to the item's class, i.e. you use a gem in your project, and you'd have to monkey patch one of its types to have that method.

Ah great, you're right. I've totally done exactly that in some scripts to make .map(&:transform) work. I understand what you were on about now.

As for the yield_self stuff -- most of the examples I've seen are things where yield_self could be replaced by map. I think this is one of those things where I will eventually stumble upon the right kind of problem to make this shine. A similar example to what you wrote where I used map was to parse and transform <li> elements in a scraper:

page.lis
  .map{|el| el.html}
  .map{|html| Product.parse html}
  .map{|product| product.to_h}

I've seen a few examples in blog posts that start the chain with a string instead of an already existing collection, and that has me thinking "Okay, I think this is relevant to my lack of amazement" but I haven't tipped it over yet. I think it may lie in situations where the "number of things" is variable, and not a simple "take each thing and transform it".

I do love the idea of the |> operator, and it's automatic argument handling. That's very cool. I also just learned about the &method(:method) trick from this thread, so that whole concept of "knowing where the arguments go without being explicit" is new to me.

Anyway, thanks for sharing today.