Nicholas Milkovits
4/2/2007 1:17:00 PM
I think the aliases are there for a good syntactic reason. It seems
to me that if you are just filtering out items which meet a certain
criteria from one array to be put into a new one (say all elements
which are even numbers) then you are collecting. If you're actually
performing a transformation on the elements then you are mapping one
array space onto another.
example
arr = [1, 2, 3, 4, 5]
arr.collect { |x| if x%2 == 0 then x end }.compact
=> [2, 4]
# Seems like maybe it should be .filter instead of .collect
arr = [1, 2, 3, 4, 5]
arr.map { |x| x*2 }
=> [2, 4, 6, 8, 10]
On 4/2/07, RubyTalk@gmail.com <rubytalk@gmail.com> wrote:
> Looking in the old archives of ruby-talk I found a thread in 2005
> about using map or collect. As far as I know there is not difference
> between the two methods. I would like to know what everyone's
> preference is?
>
> I like using collect.
>
>
> Stephen Becker IV
>
>