Object#select and method_missing

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

On May 16, 9:21 pm, Bob A. [email protected] 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?

method_missing catches public method calls, not private function
calls.

T.

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.

On May 16, 2008, at 6:21 PM, Bob A. 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

Did you mean def Kern.sel or def sel?

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

Bob A.

Bob A. 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.

Ray B. wrote:

On May 16, 2008, at 6:21 PM, Bob A. 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

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

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

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?

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

ts wrote:

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

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”?

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

Bob A. 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?

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

 See +Kernel#select+.

Also in pickaxe2, it says all instance methods of Object come from
Kernel, and it doesn’t list select() as an instance method of Object.

7stud – wrote:

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs