[#83328] tcltklib and not init'ing tk — aakhter@... (Aamer Akhter)

Hello,

13 messages 2003/10/01

[#83391] mixing in class methods — "Mark J. Reed" <markjreed@...>

Okay, probably a dumb question, but: is there any way to define

22 messages 2003/10/01
[#83392] Re: mixing in class methods — Ryan Pavlik <rpav@...> 2003/10/01

On Thu, 2 Oct 2003 06:02:32 +0900

[#83397] Re: mixing in class methods — Gavin Sinclair <gsinclair@...> 2003/10/01

On Thursday, October 2, 2003, 7:08:00 AM, Ryan wrote:

[#83399] Re: mixing in class methods — "Mark J. Reed" <markjreed@...> 2003/10/02

On Thu, Oct 02, 2003 at 07:37:25AM +0900, Gavin Sinclair wrote:

[#83404] Re: mixing in class methods — "Gavin Sinclair" <gsinclair@...> 2003/10/02

> On Thu, Oct 02, 2003 at 07:37:25AM +0900, Gavin Sinclair wrote:

[#83416] C or C++? — "Joe Cheng" <code@...>

I'd like to start writing Ruby extensions. Does it make a difference

32 messages 2003/10/02
[#83435] Re: C or C++? — "Aleksei Guzev" <aleksei.guzev@...> 2003/10/02

[#83448] xml in Ruby — paul vudmaska <paul_vudmaska@...> 2003/10/02

The biggest problem i have with Ruby is the sleepness

[#83455] Re: xml in Ruby — Chad Fowler <chad@...> 2003/10/02

On Thu, 2 Oct 2003, paul vudmaska wrote:

[#83464] Re: xml in Ruby or no xml it's just a question — paul vudmaska <paul_vudmaska@...> 2003/10/02

>>--------

[#83470] Re: xml in Ruby — paul vudmaska <paul_vudmaska@...>

>>>

15 messages 2003/10/02

[#83551] xml + ruby — paul vudmaska <paul_vudmaska@...>

>>---------

20 messages 2003/10/03
[#83562] Re: xml + ruby — Austin Ziegler <austin@...> 2003/10/03

On Fri, 3 Oct 2003 16:11:46 +0900, paul vudmaska wrote:

[#83554] hash of hashes — Paul Argentoff <argentoff@...>

Hi all.

18 messages 2003/10/03

[#83675] fox-tool - interactive gui builder for fxruby — henon <user@...>

hi fellows,

15 messages 2003/10/05

[#83730] Re: Enumerable#inject is surprising me... — "Weirich, James" <James.Weirich@...>

> Does it surprise you?

17 messages 2003/10/06
[#83732] Re: Enumerable#inject is surprising me... — nobu.nokada@... 2003/10/07

Hi,

[#83801] Extension Language for a Text Editor — Nikolai Weibull <ruby-talk@...>

OK. So I'm going to write a text editor for my masters' thesis. The

35 messages 2003/10/08
[#83803] Re: Extension Language for a Text Editor — Ryan Pavlik <rpav@...> 2003/10/08

On Thu, 9 Oct 2003 05:06:32 +0900

[#83806] Re: Extension Language for a Text Editor — Nikolai Weibull <ruby-talk@...> 2003/10/08

* Ryan Pavlik <rpav@mephle.com> [Oct, 08 2003 22:30]:

[#83812] Re: Extension Language for a Text Editor — Ryan Pavlik <rpav@...> 2003/10/08

On Thu, 9 Oct 2003 06:09:29 +0900

[#83955] Re: Extension Language for a Text Editor — Nikolai Weibull <ruby-talk@...> 2003/10/09

* Ryan Pavlik <rpav@mephle.com> [Oct, 09 2003 09:10]:

[#84169] General Ruby Programming questions — Simon Kitching <simon@...>

21 messages 2003/10/15
[#84170] Re: General Ruby Programming questions — Florian Gross <flgr@...> 2003/10/15

Simon Kitching wrote:

[#84172] Re: General Ruby Programming questions — Simon Kitching <simon@...> 2003/10/15

Hi Florian..

[#84331] Re: Email Harvesting — Greg Vaughn <gvaughn@...>

Ryan Dlugosz said:

17 messages 2003/10/21
[#84335] Re: Email Harvesting — Hugh Sasse Staff Elec Eng <hgs@...> 2003/10/21

On Wed, 22 Oct 2003, Greg Vaughn wrote:

[#84343] Re: Email Harvesting — Ruben Vandeginste <Ruben.Vandeginste@...> 2003/10/22

On Wed, 22 Oct 2003 08:35:32 +0900, Hugh Sasse Staff Elec Eng

[#84341] Ruby-oriented Linux distro? — Hal Fulton <hal9000@...>

There's been some talk of something like this in the past.

15 messages 2003/10/22
[#84348] Re: Ruby-oriented Linux distro? — Gavin Sinclair <gsinclair@...> 2003/10/22

On Wednesday, October 22, 2003, 6:01:16 PM, Hal wrote:

[#84351] Re: Ruby-oriented Linux distro? — Andrew Walrond <andrew@...> 2003/10/22

On Wednesday 22 Oct 2003 11:02 am, Gavin Sinclair wrote:

[#84420] Struggling with variable arguments to block — "Gavin Sinclair" <gsinclair@...>

Hi -talk,

18 messages 2003/10/24
[#84428] Re: Struggling with variable arguments to block — matz@... (Yukihiro Matsumoto) 2003/10/24

Hi,

[#84604] ruby-dev summary 21637-21729 — Takaaki Tateishi <ttate@...>

Hello,

21 messages 2003/10/30
[#84787] Re: ruby-dev summary 21637-21729 — Paul Brannan <pbrannan@...> 2003/11/06

On Fri, Oct 31, 2003 at 07:01:28AM +0900, Takaaki Tateishi wrote:

[#84789] Re: ruby-dev summary 21637-21729 — matz@... (Yukihiro Matsumoto) 2003/11/06

Hi,

[#84792] Re: ruby-dev summary 21637-21729 — Paul Brannan <pbrannan@...> 2003/11/06

On Thu, Nov 06, 2003 at 11:17:59PM +0900, Yukihiro Matsumoto wrote:

[#84794] Re: ruby-dev summary 21637-21729 — matz@... (Yukihiro Matsumoto) 2003/11/06

Hi,

Re: Making == symmetric?

From: elbows@... (Nathan Weston)
Date: 2003-10-01 17:21:44 UTC
List: ruby-talk #83361
"Robert Klemme" <bob.news@gmx.net> wrote in message news:<ble00n$b7qp2$2@ID-52924.news.uni-berlin.de>...
> "Ben Giddings" <bg-rubytalk@infofiend.com> schrieb im Newsbeitrag
> news:3F7A4EBC.20007@infofiend.com...
> > Nathan Weston wrote:

[snip]

> >
> > Maybe instead:
> >
> > def compare(a, b)
> >    retval = false
> >    if a.can_compare_to?(b)
> >      retval = a.==(b)
> >    end
> >    if retval and b.can_compare_to?(a)
> >      retval = b.==(a)
> >    end
> >
> >    return retval
> > end
> 
> Doesn't solve the overhead problem.
> 
> What one would really need is multiple dispatch:
> 
> module Kernel
>   def self.register_compare(classA, classB, &comp)
>     ( @comparisons ||= {})[ [classA, classB] ] = comp
>     @comparisons[ [classB, classA] ] = proc { |b,a| comp.call(a, b) }
>   end
> 
>   def self.compare(a, b)
>     c = @comparisons[ [a.class, b.class] ]
>     c.nil? ? a == b : c.call( a, b )
>   end
> end
> 
> 
> class Foo
>   Kernel.register_compare( self, self ) do |a,b|
>     puts "Foo == Foo"
>     false
>   end
> 
>   def ==(b)
>     Kernel.compare( self, b )
>   end
> end
> 
> 
> class Bar < Foo
>   Kernel.register_compare( self, self ) do |a,b|
>     puts "Bar == Bar"
>     false
>   end
> 
>   Kernel.register_compare( self, Foo ) do |a,b|
>     puts "Bar == Foo"
>     false
>   end
> end
> 
> f1 = Foo.new
> f2 = Foo.new
> 
> f1 == f2
> 
> b1 = Bar.new
> b2 = Bar.new
> 
> b1 == b2
> 
> b1 == f1
> f1 == b1
> 
> 
> But this has several disadvantages, too: You keep creating all those
> temporary arrays and you have the need for O(n**2) comparison operators.
> 
> Regards
> 
>     robert

That seems a bit too messy to me. Just overriding == is cleaner and
more intuitive.
How about:

def compare(a, b)
  if a.can_compare_to?(b)
    return a.==(b)
  elsif b.can_compare_to?(a)
    return b.==(a)
  end
  return false
end

I think the problem Ben's proposal is trying to avoid is something
like this:

class Foo
  def can_compare_to?(obj)
    if obj.is_a?(Bar)
      return true
    else 
    ...
  end

  #Foo thinks that it is not equal to any Bar object
  def ==(obj)
    if obj.is_a?(Bar)
      return false
    else
    ...
  end
end

class Bar
  def can_compare_to?(obj)
    if obj.is_a?(Foo)
      return true
    else
    ...
  end

  #Bar thinks that it is equal to any Foo object
  def ==(obj)
    if obj.is_a?(Foo)
      return true
    else
    ...
  end
end


Under Ben's solution:
Foo.new == Bar.new  #false
Bar.new == Foo.new  #false

So, the writer of Foo is happy, because he always gets the answer he
wanted. But the writer of Bar is unhappy, because he wanted true in
both cases.

Under my proposal
Foo.new == Bar.new  #false
Bar.new == Foo.new  #true

The writers of Foo and Bar are both unhappy, because they get the
wrong result in one case.

However, I would argue that, under both Ben's proposal and my
proposal, you get broken behavior -- it's just broken in different
ways. Either way, to get non-broken behavior, you need the writers of
Foo and Bar to come to some agreement about how their classes are
compared (which I don't think is unreasonable). And my way only does
one comparison, instead of 2.

Also, I suggest this default implentation of can_compare_to?

class Object
  def can_compare_to?(obj)
    return obj.kind_of?(self.class)
  end
end  

This way, in the common case where objects of class A are only equal
to other objects of class A, there is no need to override
can_compare_to?

The main problem I still see is the need to override can_compare_to?
at all. If this change is implemented, I expect many people will
override #== and be confused when this has no effect.

Maybe there is a way for #== to return an "I don't know" result, which
could remove the need for can_compare_to? at all. Newbies could still
break things by writing == methods that return false as a default
(instead of  "I don't know"), but this would really only be a problem
if they were writing class libraries for someone else's consumption
(which hopefully they wouldn't be doing).

Nathan

In This Thread