From: Alexander Schofield Date: 2002-02-23T13:40:53+09:00 Subject: Re: overriding operator 'or' Exactly, just for the archives though: irb(main):001:0> class C irb(main):002:1> def or(other) irb(main):003:2> puts 'boo!' irb(main):004:2> end irb(main):005:1> end nil irb(main):006:0> c = C.new # irb(main):007:0> c or 1 # irb(main):008:0> c.or 1 boo! nil Phil Tomson wrote: > > In article , > Phil Tomson wrote: > >According to "Ruby in a Nutshell" pg 18, 'or' is a nonmethod operator > >which cannot be overridden. However, it seems that I CAN override the > >'or' operator: > > > >irb(main):001:0> class Foo > >irb(main):002:1> def initialize(value) > >irb(main):003:2> @value = value > >irb(main):004:2> end > >irb(main):005:1> def or(other) > >irb(main):006:2> @value or other > >irb(main):007:2> end > >irb(main):008:1> end > >nil > >irb(main):009:0> myfoo = Foo.new(1) > ># > >irb(main):012:0> p myfoo or 0 > ># > >0 > > > > A little more investigation reveals that 'or' was not really overridden in > the code above. The original 'or' is still being called. > > Phil