On 31/01/11 22:52 +0200, [email protected] wrote:

Yes, actually it's http://rubybestpractices.com ;-) There is an article
about functional programming, which describes this.

This site is a ruby book (pdf) - by article i assume you meant chapter... i tried searching but only thing I could find was a comparison of using .inject vs using recursion. However its a pretty big book so I may well have missed the relevant bit.

Maybe it's not this book, but meta-programming one but I saw some
comparison between looping vs inject in some of them. I'll do the job today
and do detailed research in those book.

That got me curious; what *actually* is the advantage of using .inject. Don't get me wrong, I still think its pretty cool and elegant in its concise expressiveness, but most search results seem to suggest that .inject is actually slower (for this case, it makes no difference, since the input will never really be a HUGE structure; we are talking about key:value pairs for blob metadata). The way I understood it, if you are handling large data structures, the resultant object is returned at each 'loop' of the inner code block, making it expensive.

Did some research and I found:

http://stackoverflow.com/questions/318854/inject-and-slowness

So yes, it seems like I was wrong and it's slow. But if I understand it 
correctly
it's just implementation issue, so in meantime (eg in Ruby 1.9) it should
be fast.
The reason why I like .inject() is that it's one of the Ruby fundamental
features and it's more 'ruby-ish' ;-) So besides that current implementation is slow, it can be improved by programmers and it's always good to use it now than refactor later (from my point of view).
But you're right that in this particular case (blob) it absolutely  doesn't
matter what approach we choose.

Anyway, it's not a blocker, just small hint (I found myself .inject very
useful when I was doing some meta programming in client library. It saves
variables and lines of code (and it's better for garbage collecting)).

In this particular case it doesn't save variables ( I assume you're referring to the instantiation of the resulting structure, 'user_meta' in this case). Do you have a reference/article/explanation for the 'better for garbage collecting' (aka, please give me a reason to use .inject rather than .each, since the former is clearly much cooler),

I did some research about that and I found this 'hint':

When is each useful? Simple: when you want to create side-effects, like
saving to the database, printing a result, or sending a web service call.
In these cases, you’re not concerned with the return value; you want to
change state on the screen, the disk, the database, or something else.

But don’t use each if you want to extract some new value from an array.
That’s not what it’s for. Instead, take a look at three other powerful
functions: map, inject, or select.

http://railspikes.com/2008/7/29/functional-loops-in-ruby-each-map-inject-select-and-for

 -- Michal



marios


--
--------------------------------------------------------
Michal Fojtik, [email protected]
Deltacloud API: http://deltacloud.org
--------------------------------------------------------

Reply via email to