From: Alder Green Date: 2006-05-26T18:10:42+09:00 Subject: Re: Method '!=' On 5/26/06, Justin Collins wrote: > > > Alder Green wrote: > > On 5/26/06, Dave Burt wrote: > >> On 5/26/06, Alder Green wrote: > >> > Trying to define a method != doesn't wfm, even with define_method: > >> > > >> > irb(main):013:0> class Object; define_method(:!=) {|arg| self != > >> arg }; end > >> > class Object; define_method(:!=) {|arg| self != arg }; end > >> > > >> > SyntaxError: compile error > >> > (irb):13: parse error, unexpected tNEQ, expecting tSTRING_CONTENT or > >> > tSTRING_DBEG or tSTRING_DVAR or tSTRING_END > >> > class Object; define_method(:!=) {|arg| self != arg }; end > >> > ^ > >> > >> No, just that symbol syntax. :!= is not a valid symbol. "!=" will work > >> just fine, though. > >> > >> irb(main):001:0> class << self > >> irb(main):002:1> define_method("!=") {} > >> irb(main):003:1> end > >> => # > >> > >> Cheers, > >> Dave > > > > Yup, good tip. However, the relations between the operator syntax and > > the method calls reveals itself to be more mysterious than I thought: > > > > class Foo > > define_method('!=') { 'Foo#!= was called' } > > define_method('==') { 'Foo#== was called' } > > end > > > > irb(main):003:0> foo = Foo.new > > # > > > > irb(main):004:0> foo != 1 > > false # looks like 'foo != arg' isn't simply syntactic sugar for > > !foo.==(arg) > > > > Actually, it still is: > > foo.==(1) returns the string 'Foo#== was called' which is true. Then it > is negated: false. > > Try changing it to: > > class Foo > define_method('!=') { puts 'Foo#!= was called' } > define_method('==') { puts 'Foo#== was called' } > end > > And you will see it call '=='. > > -Justin > Do'h! You are correct, of course. -- -Alder