Const_missing

Hello!

I want to create class methods at run-time, so I use
Object#const_missing. But, I must to know the name of
the class method which was called. Is there a way to do
this via the Ruby C API (or in Ruby in general)?

I.e. when the user calls Foo.bar, I need to know (inside
const_missing) that somebody tried to call the class
method 'bar' of Foo.

Regards,

···

--
University of Athens I bet the human brain
Physics Department is a kludge --Marvin Minsky

Elias Athanasopoulos wrote:

Hello!

I want to create class methods at run-time, so I use
Object#const_missing. But, I must to know the name of
the class method which was called. Is there a way to do this via the Ruby C API (or in Ruby in general)?

I.e. when the user calls Foo.bar, I need to know (inside
const_missing) that somebody tried to call the class
method 'bar' of Foo.

Is this what you want?

class Object
   def self.const_missing(name)
     const_set(name, kl = Class.new)
     class << kl
       def method_missing(m, *args)
         puts "tried to call #{m} in #{self}"
       end
     end
     kl
   end
end

Foo.bar # ==> tried to call bar in Foo

Hi --

···

On Mon, 12 Jul 2004, Elias Athanasopoulos wrote:

Hello!

I want to create class methods at run-time, so I use
Object#const_missing. But, I must to know the name of
the class method which was called. Is there a way to do
this via the Ruby C API (or in Ruby in general)?

I.e. when the user calls Foo.bar, I need to know (inside
const_missing) that somebody tried to call the class
method 'bar' of Foo.

Interesting question, but I suspect the answer is that you can't.
const_missing is called when "Foo" is encountered -- which means no
one has tried to call "bar"; execution didn't get that far.

That's my reasoning, anyway. You may get more mileage from other
answers.

David

--
David A. Black
dblack@wobblini.net

Hi --

···

On Mon, 12 Jul 2004, Joel VanderWerf wrote:

Elias Athanasopoulos wrote:
> Hello!
>
> I want to create class methods at run-time, so I use
> Object#const_missing. But, I must to know the name of
> the class method which was called. Is there a way to do
> this via the Ruby C API (or in Ruby in general)?
>
> I.e. when the user calls Foo.bar, I need to know (inside
> const_missing) that somebody tried to call the class
> method 'bar' of Foo.

Is this what you want?

class Object
   def self.const_missing(name)
     const_set(name, kl = Class.new)
     class << kl
       def method_missing(m, *args)
         puts "tried to call #{m} in #{self}"
       end
     end
     kl
   end
end

Foo.bar # ==> tried to call bar in Foo

*Much* cooler than my answer! :slight_smile:

David

--
David A. Black
dblack@wobblini.net

David A. Black wrote:

Hi --

Elias Athanasopoulos wrote:
> Hello!
>
> I want to create class methods at run-time, so I use
> Object#const_missing. But, I must to know the name of
> the class method which was called. Is there a way to do
> this via the Ruby C API (or in Ruby in general)?
>
> I.e. when the user calls Foo.bar, I need to know (inside
> const_missing) that somebody tried to call the class
> method 'bar' of Foo.

Is this what you want?

class Object
   def self.const_missing(name)
     const_set(name, kl = Class.new)
     class << kl
       def method_missing(m, *args)
         puts "tried to call #{m} in #{self}"
       end
     end
     kl
   end
end

Foo.bar # ==> tried to call bar in Foo

*Much* cooler than my answer! :slight_smile:

Or in C:

static VALUE
class_method_missing(int argc, VALUE * argv, VALUE klass)
{
        char * methodName = rb_id2name(SYM2ID(argv[0]));
....

-- Richard

···

On Mon, 12 Jul 2004, Joel VanderWerf wrote:

Hello!

···

On Mon, Jul 12, 2004 at 08:32:24PM +0900, Richard Dale wrote:

Or in C:

static VALUE
class_method_missing(int argc, VALUE * argv, VALUE klass)
{
        char * methodName = rb_id2name(SYM2ID(argv[0]));
....

Right. This is exactly what I was looking for. Thanks a lot
(to the other posters also).

Regards,
--
University of Athens I bet the human brain
Physics Department is a kludge --Marvin Minsky