franzejr / best-ruby

Ruby Tricks, Idiomatic Ruby, Refactoring and Best Practices
http://franzejr.github.io/best-ruby/
2.39k stars 219 forks source link

Suggests reduce vs inject, but they're the same method #37

Open krainboltgreene opened 8 years ago

krainboltgreene commented 8 years ago

The chapter on combining objects suggests that reduce and inject are different methods for different tasks.

franzejr commented 8 years ago

I think it was @rafaelsales who did this. Both are not combining the elements? This part here? http://best-ruby.com/idiomatic_ruby/combine_elements_in_collection.html

imogenkinsman commented 8 years ago

It's very unclear in the example that reduce and inject are aliases.

rafaelsales commented 8 years ago

It's unclear just as in ruby documentation: http://ruby-doc.org/core-2.2.3/Enumerable.html

Both words also has 6 letters. Why? Have you seen the examples?

Why? Because the different names help understand the intention of the code. It's really about writing idiomatic ruby code. My examples show patterns of code that should be either refactored using reduce or inject word

krainboltgreene commented 8 years ago

Idiomatic ruby doesn't necessarily mean semantic or good practice. I guess this book should figure out which it cares more about: Idiomatic or semantic.

rafaelsales commented 8 years ago

The language makes available both reduce and inject words and clearly states different examples for each of them. So what can we do to help people choosing between them and write the Best Ruby code? Let's show the choices we have in the language that produce same result, and state which ones are good choices in which situations.

krainboltgreene commented 8 years ago

Are we looking at different examples? They look the same to me.

rafaelsales commented 8 years ago

They are not. Compare the examples in the book with the examples in ruby doc. Basically you won't find the use of numbers.inject(:+);

krainboltgreene commented 8 years ago

Oh, you're comparing the Best Ruby book vs the Documenation. Yes, of course, that's my point. Best Ruby suggests there's a difference implicitly where as there is zero difference in the documentation.

franzejr commented 8 years ago

@krainboltgreene I think I got your point. You are saying we are not making the BEST or at least a better idiomatic approach, we are just saying inject and reduce can do the same thing.

franzejr commented 8 years ago

Another point of view can be: How can I combine two elements? I can use inject and reduce and those are idiomatic ruby codes. I think, at the beginning this was the point and right now I think you are asking us what is the BEST or even the most idiomatic ruby code. I don't know the answer, what do you think? Probably we should define what is idiomatic ruby. I can combine two or more elements in collections using a non-idiomatic ruby code, but If I use inject OR reduce, I will use an idiomatic ruby code.

franzejr commented 8 years ago

In this section: "idiomatic ruby code", we should create a sort of idiomatic ruby code, it means: I can do this thing using a non-idiomatic ruby code, BUT I can also use some approaches in the language and that will look more idiomatic ruby, but it does not mean this is the best approach OR this is the BEST idiomatic ruby code. What do you guys think?

rafaelsales commented 8 years ago

@krainboltgreene Why the documentation uses reduce(:+) in both methods examples? And why the doc. uses blocks only for inject in both methods examples? Why the documentation doesn't even mention one is the alias of the other? And finally why both exist if the word reduce is the popular one out there?

krainboltgreene commented 8 years ago

Why the documentation uses reduce(:+) in both methods examples?

Largely because documentation is boring and cruby core documentation has a huge amount of hoops to jump through, so Copy/Paste becomes the most efficient solution.

Why the documentation doesn't even mention one is the alias of the other?

Because it probably appeared obvious to the writer.

And finally why both exist if the word reduce is the popular one out there?

Probably the same reason we have File.write and File.open { |f| f.write } and File.read and File.open().read (though the last one has different side effects). Ruby is an language built on top of many other language idioms. I doubt there's an intent in the design.

For what it's worth I would just talk about reduce and be done with it. I mean I learned how to merge objects doing list.inject(:merge), so...