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 Aman

···

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

method_missing catches public method calls, not private function
calls.

T.

···

On May 16, 9:21 pm, Bob Aman <b...@sporkmonger.com> 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

···

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?

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-forum.com/\.

Ray Baxter 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

···

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

--
Posted via http://www.ruby-forum.com/\.

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-forum.com/\.

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-forum.com/\.

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-forum.com/\.

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-forum.com/\.

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

--
Posted via http://www.ruby-forum.com/\.

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

--
Posted via http://www.ruby-forum.com/\.