From: Alder Green Date: 2006-05-26T17:12:22+09:00 Subject: Re: Method '!=' 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) irb(main):005:0> foo.== "Foo#== was called" irb(main):008:0> foo.send '!=' "Foo#!= was called" # And send seems to be the only way to call != But as far as Victor is concerned, as long as #send is used, those methods would work as expected. -- -Alder