From: Brian Candler Date: 2007-06-01T22:50:18+09:00 Subject: Re: One More Bias (or perhaps I'm too novice ) On Fri, Jun 01, 2007 at 10:39:23PM +0900, Vin Raja wrote: > 1 class Person > 2 def *(o) > 3 puts "One person meets another" > 4 end > 5 > 6 def meets (o) > 7 puts "One person meets another" > 8 end > 9 end > > 10 a= Person.new > 11 b=Person.new > 12 > 13 a * b > 14 a.meets b > > ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ > now with above Class the output is: > >ruby yet.rb > One person meets another > One person meets another > > CASE 1 > If I replace Line 13 with this: > a % b #(or any other operator i.e. $ ^ & et al.) > > I get the following error : > yet.rb:16: undefined method `%' for # (NoMethodError) Correct. a % b is syntactic sugar for a.%(b) or a.send(:%, b) That is, invoke a method called '%' on object 'a', passing 'b' as the argument. > > CASE 2 > But when i replaced Line 14 with this: > a meets b > the error was this > > >ruby yet.rb > One person meets another > yet.rb:17: warning: parenthesize argument(s) for future version > yet.rb:17: undefined method `meets' for main:Object (NoMethodError) If you don't follow 'a' by a dot, then it's treated as a method name on the local object: that is, self.a(...) So what you have written is a( meets( b ) ) which has an implicit receiver, self.a( meets( b ) ) The receiver, self, is a top-level object called "main". But you have not defined methods 'a' or 'meets' at the top level. def a(x) "a:#{x}" end def meets(x) "meets:#{x}" end b = "fred" puts(a meets b) # "a:meets:fred" but with warnings about missing parens puts a(meets(b)) # no warnings Brian.