Object#select and method_missing

B

Bob Aman

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
 
T

Trans

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.
 
B

Bob Aman

method_missing catches public method calls, not private function

That doesn't really answer my question. I want to know why select()
behaves differently from self.select() in this context.
 
R

Ray Baxter

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

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
 
7

7stud --

Ray said:
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
 
B

Bob Aman

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
 
B

Bob Aman

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
 
T

ts

7stud said:
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
 
7

7stud --

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


$ ri Object#select
Nothing known about Object#select

$ ri Kernel#select
---------------------------------------------------------- Kernel#select
IO.select(read_array
[, write_array
[, error_array
[, timeout]]] ) => array or nil
 
7

7stud --

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

pickaxe2 says the the types of methods in a Module are:

1) module methods(i.e. like class methods)
2) instance methods.

I guess to make things more confusing, you are calling the module
methods "singleton methods". I understand why. But why are you calling
the instance methods "private methods"?
 
T

ts

7stud said:
I guess to make things more confusing, you are calling the module
methods "singleton methods". I understand why. But why are you calling
the instance methods "private methods"?

To take your example, it do this

vgs% cat b.rb
#!/usr/bin/ruby
module Kern

def Kern.sel
puts "select"
end

private
def 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
vgs%

vgs% ./b.rb
meth1
method missing
meth2
select
vgs%



Guy Decoux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top