From: Anderson Leite on
How can I compare two objects and get true if some of his atributes are
equals ?

I need to compare two arrays of users, and get and third array just with
the matches. I found the "&" method that work for Fixnuns and String,
but...how to use with objects ?


class User
attr_accessor :email
end

a = User.new
a.email = 'ruby(a)rails.com'

b = User.new
b.email = 'ruby(a)rails.com'

array_one = [a]
array_two = [b]


array_three = array_one & array_two

puts array_three # I want the user here



Can you help me ?
thanks
--
Posted via http://www.ruby-forum.com/.

From: Marcin Wolski on
Anderson Leite wrote:
> How can I compare two objects and get true if some of his atributes are
> equals ?
>
> I need to compare two arrays of users, and get and third array just with
> the matches. I found the "&" method that work for Fixnuns and String,
> but...how to use with objects ?
>
>
> class User
> attr_accessor :email
> end
>
> a = User.new
> a.email = 'ruby(a)rails.com'
>
> b = User.new
> b.email = 'ruby(a)rails.com'
>
> array_one = [a]
> array_two = [b]
>
>
> array_three = array_one & array_two
>
> puts array_three # I want the user here
>
>
>
> Can you help me ?
> thanks

I think you need to define how to compare your objects and their hash.
Have a look below.


class User
attr_accessor :email

def ==(other)
@email == other.email
end

alias eql? ==

def hash
code = 17
code = 37*code + @email.hash
end

def to_s
"#@email"
end

end

a = User.new
a.email = 'ruby(a)rails.com'

c = User.new
c.email = 'groovy(a)rails.com'

b = User.new
b.email = 'ruby(a)rails.com'

d = User.new
d.email = 'c++(a)rails.com'

d = User.new
d.email = 'python(a)rails.com'

array_one = [a,d]
array_two = [b, c]


array_three = array_one & array_two

puts array_three # I want the user here
#prints ruby(a)rails.com
--
Posted via http://www.ruby-forum.com/.

From: Robert Dober on
On Fri, Jun 4, 2010 at 3:41 AM, Marcin Wolski <wolskint(a)o2.pl> wrote:
<snip>
>  def ==(other)
>  @email == other.email
>  end
this seems ok
>
>  alias eql? ==
>
>  def hash
>    code = 17
>    code = 37*code + @email.hash
> end
>
Are you sure you want to do this? "equal" objects would overwrite each
other when used as hash keys. This is normally not a good idea, be
sure you really want/need this!
R


--
The best way to predict the future is to invent it.
-- Alan Kay

From: Marcin Wolski on
Robert Dober wrote:
> On Fri, Jun 4, 2010 at 3:41 AM, Marcin Wolski <wolskint(a)o2.pl> wrote:
> <snip>
>> �def ==(other)
>> �@email == other.email
>> �end
> this seems ok
>>
>> �alias eql? ==
>>
>> �def hash
>> � �code = 17
>> � �code = 37*code + @email.hash
>> end
>>
> Are you sure you want to do this? "equal" objects would overwrite each
> other when used as hash keys. This is normally not a good idea, be
> sure you really want/need this!
> R

What would be the other possible solution to this problem? The example
code will not work without overwritten eql? and hash methods.
--
Posted via http://www.ruby-forum.com/.

From: Robert Dober on
On Fri, Jun 4, 2010 at 10:16 AM, Marcin Wolski <wolskint(a)o2.pl> wrote:
> Robert Dober wrote:
>> On Fri, Jun 4, 2010 at 3:41 AM, Marcin Wolski <wolskint(a)o2.pl> wrote:
>> <snip>
>>> �def ==(other)
>>> �@email == other.email
>>> �end
>> this seems ok
>>>
>>> �alias eql? ==
>>>
>>> �def hash
>>> � �code = 17
>>> � �code = 37*code + @email.hash
>>> end
>>>
>> Are you sure you want to do this? "equal" objects would overwrite each
>> other when used as hash keys. This is normally not a good idea, be
>> sure you really want/need this!
>> R
>
> What would be the other possible solution to this problem? The example
> code will not work without overwritten eql? and hash methods.
> --
> Posted via http://www.ruby-forum.com/.
>
Does it not? Seems to work on my box
class A
def == other
true
end
end # class A

a1 = A::new
a2 = A::new
a3 = A::new

p [a1,a2] & [a1,a3]



--
The best way to predict the future is to invent it.
-- Alan Kay