[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Object#select and method_missing

Bob Aman

5/17/2008 1:22:00 AM

class SelectTest
def method_missing(method, *params, &block)
return "Expected Result" if method == :select
super
end

def test_one
self.select()
end

def test_two
select()
end
end
puts SelectTest.new.test_one
puts SelectTest.new.test_two


Anyone care to explain why test_one calls method_missing and test_two
calls the private method Object#select?

- Bob Aman
--
Posted via http://www.ruby-....

13 Answers

Trans

5/17/2008 2:07:00 AM

0



On May 16, 9:21=A0pm, Bob Aman <b...@sporkmonger.com> wrote:
> class SelectTest
> =A0 def method_missing(method, *params, &block)
> =A0 =A0 return "Expected Result" if method =3D=3D :select
> =A0 =A0 super
> =A0 end
>
> =A0 def test_one
> =A0 =A0 self.select()
> =A0 end
>
> =A0 def test_two
> =A0 =A0 select()
> =A0 end
> end
> puts SelectTest.new.test_one
> puts SelectTest.new.test_two
>
> Anyone care to explain why test_one calls method_missing and test_two
> calls the private method Object#select?

method_missing catches public method calls, not private function
calls.

T.

Bob Aman

5/17/2008 2:10:00 AM

0

> method_missing catches public method calls, not private function
> calls.

That doesn't really answer my question. I want to know why select()
behaves differently from self.select() in this context.
--
Posted via http://www.ruby-....

Ray Baxter

5/17/2008 3:25:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

On May 16, 2008, at 6:21 PM, Bob Aman wrote:

> class SelectTest
> def method_missing(method, *params, &block)
> return "Expected Result" if method == :select
> super
> end
>
> def test_one
> self.select()
> end
>
> def test_two
> select()
> end
> end
> puts SelectTest.new.test_one
> puts SelectTest.new.test_two
>
>
> Anyone care to explain why test_one calls method_missing and test_two
> calls the private method Object#select?

Object#select is a private method. Private methods cannot be called with
an explicit receiver. Your call self.select has an explicit receiver.

Sounds like there should be a special exception for when the receiver is
self, but I could be missing something.

Ray

7stud --

5/17/2008 4:27:00 AM

0

Ray Baxter wrote:
> On May 16, 2008, at 6:21 PM, Bob Aman wrote:
>
>> def test_two
>> select()
>> end
>> end
>> puts SelectTest.new.test_one
>> puts SelectTest.new.test_two
>>
>>
>> Anyone care to explain why test_one calls method_missing and test_two
>> calls the private method Object#select?
>
> Object#select is a private method. Private methods cannot be called with
> an explicit receiver. Your call self.select has an explicit receiver.
>
> Sounds like there should be a special exception for when the receiver is
> self, but I could be missing something.
>
> Ray

Why do both the method calls: sel() and self.sel() work in this code?

module Kern

def Kern.sel
puts "select"
end

end


class Obj
include Kern
end


class Test < Obj
def method_missing(meth_sym, *args)
puts "method missing"
end

def meth1
puts "meth1"
self.sel
end

def meth2
puts "meth2"
sel
end
end


t = Test.new
t.meth1
t.meth2


--output:--
meth1
method missing
meth2
method missing
--
Posted via http://www.ruby-....

Bob Aman

5/17/2008 4:45:00 AM

0

> Why do both the method calls: sel() and self.sel() work in this code?
>
> module Kern
> def Kern.sel
> puts "select"
> end
> end

Did you mean def Kern.sel or def sel?

Because in the code above, def Kern.sel does basically nothing.

Bob Aman
--
Posted via http://www.ruby-....

7stud --

5/17/2008 5:33:00 AM

0

Bob Aman wrote:
>> Why do both the method calls: sel() and self.sel() work in this code?
>>
>> module Kern
>> def Kern.sel
>> puts "select"
>> end
>> end
>
> Did you mean def Kern.sel or def sel?
>

Kern.sel -- pickaxe2 says select() is a module method of Kernel.


--
Posted via http://www.ruby-....

ts

5/17/2008 11:25:00 AM

0

7stud -- wrote:
> Why do both the method calls: sel() and self.sel() work in this code?
>
> module Kern
>
> def Kern.sel
> puts "select"
> end

you define a singleton method

>
> end
>
>
> class Obj
> include Kern

include don't make the singleton methods available to the object.

> def meth1
> puts "meth1"
> self.sel

it just don't exist

> end
>
> def meth2
> puts "meth2"
> sel

same here



Guy Decoux

7stud --

5/17/2008 4:25:00 PM

0

ts wrote:
> Guy Decoux

I tried to model how Object mixes in Kernel and thereby recreate what
the op's results were. Is that not the way it works?
--
Posted via http://www.ruby-....

Bob Aman

5/17/2008 4:32:00 PM

0

> I tried to model how Object mixes in Kernel and thereby recreate what
> the op's results were. Is that not the way it works?

Afraid not. Object#select is what's being called, not Kernel.select.

Bob Aman
--
Posted via http://www.ruby-....

ts

5/17/2008 4:51:00 PM

0

7stud -- wrote:
> I tried to model how Object mixes in Kernel and thereby recreate what
> the op's results were. Is that not the way it works?

In the example given, #select is a global function this mean
* a private method of Kernel
* a singleton method of Kernel

This is to give the possibility to *all* objects to access these
methods

When it's called 'self.select', ruby find the private method but
because a receiver is specified (it's called like a public method),
it call #method_missing (this was not the right method).

When it's called 'select', it find the same method and it call it
because it was called without a receiver



Guy Decoux