On Wed, Mar 16, 2011 at 8:55 PM, Nicolas Cavigneaux
<[email protected]> wrote:
> Hi Chris,
>
> I want to be able to do things like:
>
> [ 1, 2 ].uniq?
> [ {:lang => "Ruby"}, {:lang => "ObjC"} ].uniq?
> [ 1, 1, 2 ].uniq? { |x| x > 1 }
> [ nc,dhh ].uniq? { |p| p.name }

Is it possible ? I think you sometimes want to select records using
the given proc and sometimes map them. Your current implementation
uses "select":


class Person < Struct.new(:name, :age)
end
nc = Person.new(:nc, 1)
dhh1 = Person.new(:dhh, 2)
dhh2 = Person.new(:dhh, 3)

[ nc,dhh1 ].uniq? { |p| p.name } => true
[ nc, dhh1, dhh2 ].uniq? { |p| p.name }  => true

All records are selected because p.name is evaluated to true, the
their uniquness is checked which is still true despite the fact the
their all have the same name...

[ 1, 1, 2].uniq? { |x| x > 1 } => true
[ 1, 1, 2, 2].uniq? { |x| x > 1 } => false

Works as you want.

If we change the implementation then first example will go ok but the
second one not.

We would have to have two methods. Am I right ?

uniq_by?(&:name) sounds good for me.


Robert Pankowecki

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-core?hl=en.

Reply via email to