6 ms·
The example for even numbers should be: even_numbers = [1, 2, 3, 4, 5].find_all { |element| element.even? } or even_numbers = [1, 2, 3, 4, 5].find_all(&:even
by werdnapk 9y ago
The example for even numbers should be:
even_numbers = [1, 2, 3, 4, 5].find_all { |element| element.even? }
or
even_numbers = [1, 2, 3, 4, 5].find_all(&:even?)
instead of:
even_numbers = [1, 2, 3, 4, 5].map { |element| element if element.even? }
- deleted 9y ago[deleted]
- dopamean 9y agoIf I'm writing ruby I'd prefer Array#select for this task.
- pfarrell 9y agoExactly [1,2,3,4,5].select{|x| x%2 == 0} If I were interviewing a a Ruby dev, anything other than Array#select indicates lack of experience.
- turboladen 9y agoThat seems a bit shallow. I’ve been Rubying for 10 years and have always preferred find_all over select. select turns up in other APIs having different meanings (thinking of IO specifically); find_all tells you exactly what it’s going to do. Just my opinion, of course.
- dkubb 9y agoFor me, it's always been select/reject/detect. Even though I know about find_all, it always seemed like the odd one out.
- odammit 9y agoUnrelated! I haven’t talked to you in years. I wrote some DM plugins back in the day. Hope you’re doing well! Are you working on any interesting open source projects?
- dkubb 9y agoI don't recognize your handle, but nice that you remember DataMapper! I'm doing really well, although I did kind of fall off OSS work as I got busier with kids and family stuff. I still hack on lots of different things, but nothing that I've been able to open source. I've been busy learning Haskell these days, although I tend to write Ruby for work.
- odammit 9y agoGlad all is well!
- fny 9y agoYou're forgetting the existence of `find` which easily justifies `find_all`
- pmontra 9y agoI always use select and I've been writing Ruby since 2006. I didn't even remember find_all exists. As a name select reflects what I want it to do, find_all much less (this is very subjective). Furthermore it's easier to type. Luckily we have both and everyone is happy.
- nurettin 9y agoYou know that methods in ruby can have many aliases, right? Can you clarify your statement?
- stouset 9y agoThat’s an alias for `find_all`.
- yladiz 9y agoThat's one beautiful thing about Ruby. There's not just one way to do it, which means that .select and .find_all may be aliases but both are valid and will show up, depending on the author's preference.
- danappelxx 9y agoHonestly, that sounds like a nightmare to me!
- odammit 9y agoI love Ruby, but I agree. It creates unnecessary ambiguity. Reads nicer sometimes, sure.
- dopamean 9y agoI'm aware of that. I prefer the *ect methods only because they've always seemed to be more popular amongst Rubyists.