class_missing ?

  • Thread starter Elias Athanasopoulos
  • Start date
E

Elias Athanasopoulos

Hello!

Is there something similar to method_missing for classes?

I.e. overload a method of Object or handle an exception
when a 'Foo.new' is called and class Foo does not exist.

Regards,
 
K

Kent S.

Try this:

$ irb
irb(main):001:0> defined? AClass
=> nil
irb(main):002:0> class AClass; end
=> nil
irb(main):003:0> defined? AClass
=> "constant"
irb(main):004:0>

Cheers,
Kent.
 
D

Dmitry V. Sabanin

Hello!

Is there something similar to method_missing for classes?

I.e. overload a method of Object or handle an exception
when a 'Foo.new' is called and class Foo does not exist.
Try:
# ri const_missing
 
M

Mark Hubbart

Hello!

Is there something similar to method_missing for classes?

I.e. overload a method of Object or handle an exception
when a 'Foo.new' is called and class Foo does not exist.

Something like this?

def Object.const_missing(sym)
Object.const_set( sym, Class.new() )
end
==>nil
Foo.new
==>#<Foo:0x3c408>
Bar.new
==>#<Bar:0x398d4>
Baz.new
==>#<Baz:0x21370>
 
E

Elias Athanasopoulos

Something like this?

def Object.const_missing(sym)
Object.const_set( sym, Class.new() )
end
==>nil
Foo.new
==>#<Foo:0x3c408>
Bar.new
==>#<Bar:0x398d4>
Baz.new
==>#<Baz:0x21370>

Exactly! Thanx (to Dmitry also). :)

Ruby never fails to surprise - not in the common 'bad' sense - me!

Regards,
 

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

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,371
Latest member
Brkaa

Latest Threads

Top