1def foo
2  @results = []
3  stuff.each do |thing|
4    @results << thing if is_cool?(thing)
5  end
6  
7  @results
8end

What is a better way of doing this?

posted Dec 10 '14 at 0:24

2 Refactors


1def foo
2  [].tap do |results|
3    stuff.each do |thing|
4      results << thing if is_cool?(thing)
5    end
6  end
7end

tap is sweet!

suggested Dec 10 '14 at 0:26

totally sweet.

tap tap tap it in


Preferred!

Shortest Code!

1def foo
2  @results = stuff.select { |thing| is_cool?(thing) }
3end
suggested Dec 10 '14 at 14:58

slick.