Afficher un message
Vieux 05/11/2007, 19h43   #7
bbxx789_05ss@yahoo.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Reading a class-file and calling it at runtime.

Sean O'halpin wrote:
>
> plugin_class = Object.const_get(plugin_class_name).new
>


I have a question about using Object as the receiver in the above line.
Is there any specific reason you are using Object? I find calling
const_get with Object very confusing because the Object class does not
define a const_get method--it's only tracking const_get through ruby's
extremely confusing circular inheritance:

Object <------+
| |
V |
Module |
--const_get |
|
| |
V |
Class ---------+

that const_get lands in Object. Since calling const_get with any of the
class names Object, Module, or Class seems to work, why not just use
Module or Class to call const_get? If no Modules are involved, which
seems to be the op's case, then Class would seem like the least
confusing name to call const_get with.

#plugin.rb:

class Dog
def speak
puts "Woof woof"
end
end

-----------

#my_program.rb:

require 'plugin'

dog_class = Class.const_get("Dog")
dog = dog_class.new
dog.speak

Of course calling const_get with Class might still result in some
confusion--looking up the methods for Class will not reveal a const_get
method.
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
 
Page generated in 0,06026 seconds with 9 queries