# \[n00b\] Reading a class-file and calling it at runtime

**URL:** https://rubytalk.org/t/n00b-reading-a-class-file-and-calling-it-at-runtime/41918
**Category:** ruby-talk
**Created:** [5 November 2007 02:51 UTC](https://rubytalk.org/t/n00b-reading-a-class-file-and-calling-it-at-runtime/41918 "2007-11-05T02:51:26Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![Miss\_Elaine\_Eos](https://avatars.discourse-cdn.com/v4/letter/m/76d3ee/32.png) [@Miss\_Elaine\_Eos](https://rubytalk.org/u/Miss_Elaine_Eos)
#### Post date: [5 November 2007 02:51 UTC](https://rubytalk.org/t/n00b-reading-a-class-file-and-calling-it-at-runtime/41918/1 "2007-11-05T02:51:26Z")

</div>

I'm trying to read-in a folder full of "plug-ins" and call each of them,  
in turn. Once I get the class-name, I do something like:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;require "#{PLUG\_IN\_DIR}/#{one\_plugin}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;plugin\_class=Object.const\_get(plugin\_class\_name).new  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;plugin\_class.some\_method

I \*thought\* that require worked a bit like the C pre-processor  
"include", in that it would read and execute the named file at that  
point, thereby defining my class and its methods. However, when I get  
to the middle line, I get

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;uninitialized constant PluginClassName

Since rails is mistaking my class-name for a constant, I'm guessing that  
require didn't execute the way I think it does, so my class-name isn't  
initialized.

...Or maybe I've completely mis-diagnosed the problem.

At any rate, can someone offer a suggestion for how to read a folder  
full of class-definition-files and, once at a time,

&nbsp;&nbsp;\* Execute the class definition, so that my app knows about it  
&nbsp;&nbsp;\* Instantiate an instance of the class (I think we have this part,  
above)  
&nbsp;&nbsp;\* Call a method on that class (should just be able to say  
"a\_class.a\_method", right?)

Thanks!

> **···**
>
> --  
> Please take off your pants or I won't read your e-mail.  
> I will not, no matter how "good" the deal, patronise any business which sends  
> unsolicited commercial e-mail or that advertises in discussion newsgroups.

---

<div class="post-metadata">

### Author: ![Emmanuel\_Oga2](https://avatars.discourse-cdn.com/v4/letter/e/c0e974/32.png) [@Emmanuel\_Oga2](https://rubytalk.org/u/Emmanuel_Oga2)
#### Post date: [5 November 2007 03:19 UTC](https://rubytalk.org/t/n00b-reading-a-class-file-and-calling-it-at-runtime/41918/2 "2007-11-05T03:19:57Z")

</div>

I'm not sure what you are trying to acomplish... but here is a trick i  
use when rails mess with my requires.

def require\_relative \*args  
&nbsp;&nbsp;path= File.dirname(args.shift)  
&nbsp;&nbsp;args.each { |arg| path= File.join(path, arg) }  
&nbsp;&nbsp;require path  
end

This will require the file with full file name. Now you can do

Dir[File.join(File.dirname(\_\_FILE\_\_), '/plugins/\*.rb')].each do |file|  
&nbsp;&nbsp;require\_relative \_\_FILE\_\_, file  
end

That loads every file of directory plugins. Now, if you named the files  
according to rails convetions, ie:

my\_class\_name.rb for MyClassName class, you can do something like:

plugin\_classes= []

Dir[File.join(File.dirname(\_\_FILE\_\_), '/plugins/\*.rb')].each do |file|  
&nbsp;&nbsp;require\_relative \_\_FILE\_\_, file  
&nbsp;&nbsp;name= File.basename(file)  
&nbsp;&nbsp;plugin\_classes \<\< $1.camelize.constantize if name =~ /(.\*)\.rb$/i  
end

There... it sounds easy, no? 🙂

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

### Author: ![Sean\_O\_Halpin](https://avatars.discourse-cdn.com/v4/letter/s/ac91a4/32.png) [@Sean\_O\_Halpin](https://rubytalk.org/u/Sean_O_Halpin)
#### Post date: [5 November 2007 07:49 UTC](https://rubytalk.org/t/n00b-reading-a-class-file-and-calling-it-at-runtime/41918/3 "2007-11-05T07:49:04Z")

</div>

Hi,

What you're doing here is right in principle. For example, in this code:

# ./myplugin.rb  
class MyPlugin  
&nbsp;&nbsp;def some\_method  
&nbsp;&nbsp;&nbsp;&nbsp;puts "Hi from #{self}!"  
&nbsp;&nbsp;end  
end

module Plugins  
&nbsp;&nbsp;class AnotherPlugin  
&nbsp;&nbsp;&nbsp;&nbsp;def some\_method  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Hi from #{self}!"  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end  
\_\_END\_\_

# test-require-plugin.rb  
one\_plugin = "myplugin"  
PLUG\_IN\_DIR = '.'  
plugin\_class\_name = "MyPlugin"

require "#{PLUG\_IN\_DIR}/#{one\_plugin}"  
plugin\_class = Object.const\_get(plugin\_class\_name).new  
plugin\_class.some\_method

plugin\_class\_name = "AnotherPlugin"  
plugin\_class = Plugins.const\_get(plugin\_class\_name).new  
plugin\_class.some\_method

# this will fail  
plugin\_class = Object.const\_get(plugin\_class\_name).new  
plugin\_class.some\_method

\_\_END\_\_  
# output  
Hi from #\<MyPlugin:0xb7c25594\>!  
Hi from #\<Plugins::AnotherPlugin:0xb7c23dfc\>!  
test-require-plugin.rb:14:in `const\_get': uninitialized constant  
AnotherPlugin (NameError)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from test-require-plugin.rb:14

only the last call to some\_method fails. Have you checked that the  
name of the class is correct or if you are defining your plugin class  
within a module?

Regards,  
Sean

> **···**
>
> On 11/5/07, Miss Elaine Eos \<Misc@your-pants.playnaked.com\> wrote:
> 
> > I'm trying to read-in a folder full of "plug-ins" and call each of them,  
> > in turn. Once I get the class-name, I do something like:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;require "#{PLUG\_IN\_DIR}/#{one\_plugin}"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;plugin\_class=Object.const\_get(plugin\_class\_name).new  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;plugin\_class.some\_method
> > 
> > I \*thought\* that require worked a bit like the C pre-processor  
> > "include", in that it would read and execute the named file at that  
> > point, thereby defining my class and its methods. However, when I get  
> > to the middle line, I get
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;uninitialized constant PluginClassName
> > 
> > Since rails is mistaking my class-name for a constant, I'm guessing that  
> > require didn't execute the way I think it does, so my class-name isn't  
> > initialized.
> > 
> > ...Or maybe I've completely mis-diagnosed the problem.
> > 
> > At any rate, can someone offer a suggestion for how to read a folder  
> > full of class-definition-files and, once at a time,
> > 
> > &nbsp;&nbsp;\* Execute the class definition, so that my app knows about it  
> > &nbsp;&nbsp;\* Instantiate an instance of the class (I think we have this part,  
> > above)  
> > &nbsp;&nbsp;\* Call a method on that class (should just be able to say  
> > "a\_class.a\_method", right?)
> > 
> > Thanks!
> > 
> > --  
> > Please take off your pants or I won't read your e-mail.  
> > I will not, no matter how "good" the deal, patronise any business which sends  
> > unsolicited commercial e-mail or that advertises in discussion newsgroups.

---

<div class="post-metadata">

### Author: ![Emmanuel\_Oga2](https://avatars.discourse-cdn.com/v4/letter/e/c0e974/32.png) [@Emmanuel\_Oga2](https://rubytalk.org/u/Emmanuel_Oga2)
#### Post date: [5 November 2007 03:22 UTC](https://rubytalk.org/t/n00b-reading-a-class-file-and-calling-it-at-runtime/41918/4 "2007-11-05T03:22:24Z")

</div>

mmm i believe i messed up the things a bit, this should be the right  
thing to do, if complex, anyway:

plugin\_classes=

Dir[File.join(File.dirname(\_\_FILE\_\_), '/plugins/\*.rb')].each do |file|

&nbsp;&nbsp;file\_name= File.basename(file)

&nbsp;&nbsp;require\_relative \_\_FILE\_\_, 'plugins', file\_name

&nbsp;&nbsp;plugin\_classes \<\< $1.camelize.constantize if file\_name =~ /(.\*)\.rb$/i  
end

There, now it should work! 🙂

> **···**
>
> > plugin\_classes=
> > 
> > Dir[File.join(File.dirname(\_\_FILE\_\_), '/plugins/\*.rb')].each do |file|  
> > &nbsp;&nbsp;require\_relative \_\_FILE\_\_, file  
> > &nbsp;&nbsp;name= File.basename(file)  
> > &nbsp;&nbsp;plugin\_classes \<\< $1.camelize.constantize if name =~ /(.\*)\.rb$/i  
> > end
> > 
> > There... it sounds easy, no? 🙂
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

### Author: ![Miss\_Elaine\_Eos](https://avatars.discourse-cdn.com/v4/letter/m/76d3ee/32.png) [@Miss\_Elaine\_Eos](https://rubytalk.org/u/Miss_Elaine_Eos)
#### Post date: [5 November 2007 16:15 UTC](https://rubytalk.org/t/n00b-reading-a-class-file-and-calling-it-at-runtime/41918/5 "2007-11-05T16:15:03Z")

</div>

In article  
\<3736dd30711042349n7225229dw4af74110f2a52048@mail.gmail.com\>,

> **···**
>
> sean.ohalpin@gmail.com wrote:
> 
> > only the last call to some\_method fails. Have you checked that the  
> > name of the class is correct or if you are defining your plugin class  
> > within a module?
> 
> I am NOT using "module." I guess I need to go read-up on modules --  
> Thanks! 🙂
> 
> --  
> Please take off your pants or I won't read your e-mail.  
> I will not, no matter how "good" the deal, patronise any business which sends  
> unsolicited commercial e-mail or that advertises in discussion newsgroups.

---

<div class="post-metadata">

### Author: ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)
#### Post date: [5 November 2007 18:43 UTC](https://rubytalk.org/t/n00b-reading-a-class-file-and-calling-it-at-runtime/41918/6 "2007-11-05T18:43:39Z")

</div>

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 \<------+  
&nbsp;&nbsp;\> \>  
&nbsp;&nbsp;V |  
Module |  
--const\_get |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\>  
&nbsp;&nbsp;\> \>  
&nbsp;&nbsp;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  
&nbsp;&nbsp;def speak  
&nbsp;&nbsp;&nbsp;&nbsp;puts "Woof woof"  
&nbsp;&nbsp;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/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

### Author: ![Miss\_Elaine\_Eos](https://avatars.discourse-cdn.com/v4/letter/m/76d3ee/32.png) [@Miss\_Elaine\_Eos](https://rubytalk.org/u/Miss_Elaine_Eos)
#### Post date: [6 November 2007 06:05 UTC](https://rubytalk.org/t/n00b-reading-a-class-file-and-calling-it-at-runtime/41918/7 "2007-11-06T06:05:05Z")

</div>

In article  
\<3736dd30711042349n7225229dw4af74110f2a52048@mail.gmail.com\>,

> \> I'm trying to read-in a folder full of "plug-ins" and call each of them,  
> \> in turn. Once I get the class-name, I do something like:  
> \>  
> \> require "#{PLUG\_IN\_DIR}/#{one\_plugin}"  
> \> plugin\_class=Object.const\_get(plugin\_class\_name).new  
> \> plugin\_class.some\_method

&nbsp;&nbsp;&nbsp;[snip]

Ok, I tried your example, below, and it worked the same for me as for  
you. But when I go back to my actual code, same problem. I don't  
follow what's different about what your example is doing and what I'm  
doing. Here are some differences:

\* I'm running under rails. Can't see how that should matter -- this is  
long before any of the rendering stuff, and no DB accesses. I'm just  
triggering code in a controller. That's all ruby, right?

\* My program works like your example, below, except that it walks a  
directory and does the trick for every file in it. So, rather than call  
"myplugin.rb", it walks the directory, finds my\_plugin.rb, uses that as  
a filename, converts to MyPlugin for class name, but the rest is the  
same.

\* My actual plugin is a sub-class of a master-plugin. This is how I  
guarantee the interface. (There's probably a more Ruby-like way to do  
this, but it's what I know...) So myplugin is mildly complicated in  
that my 1st line looks more like this:

&nbsp;&nbsp;class MyPlugin \< MasterPlugin

But modifying the sample (below) to replicate that doesn't seem to  
impact it's working-ness.

So I'm stuck trying to figure out how & where to trim back my original  
to get it to act like the example. Does Rails actually matter?! Does  
it matter if my require is inside a loop and/or inside another class (a  
rails controller)?

The problem: I get

&nbsp;&nbsp;&nbsp;uninitialized constant MyPlugin

at the line with const\_get().

&nbsp;&nbsp;&nbsp;[Example trimmed back to the minimal case that I'm using]

> **···**
>
> sean.ohalpin@gmail.com wrote:
> 
> > On 11/5/07, Miss Elaine Eos \<Misc@your-pants.playnaked.com\> wrote:  
> > What you're doing here is right in principle. For example, in this code:
> > 
> > # ./myplugin.rb  
> > class MyPlugin  
> > &nbsp;&nbsp;def some\_method  
> > &nbsp;&nbsp;&nbsp;&nbsp;puts "Hi from #{self}!"  
> > &nbsp;&nbsp;end  
> > end  
> > \_\_END\_\_
> > 
> > # test-require-plugin.rb  
> > one\_plugin = "myplugin"  
> > PLUG\_IN\_DIR = '.'  
> > plugin\_class\_name = "MyPlugin"
> > 
> > require "#{PLUG\_IN\_DIR}/#{one\_plugin}"  
> > plugin\_class = Object.const\_get(plugin\_class\_name).new  
> > plugin\_class.some\_method
> > 
> > plugin\_class\_name = "AnotherPlugin"  
> > plugin\_class = Plugins.const\_get(plugin\_class\_name).new  
> > plugin\_class.some\_method  
> > \_\_END\_\_  
> > # output  
> > Hi from #\<MyPlugin:0xb7c25594\>!  
> > Hi from #\<Plugins::AnotherPlugin:0xb7c23dfc\>!
> 
> --  
> Please take off your pants or I won't read your e-mail.  
> I will not, no matter how "good" the deal, patronise any business which sends  
> unsolicited commercial e-mail or that advertises in discussion newsgroups.

---

<div class="post-metadata">

### Author: ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)
#### Post date: [5 November 2007 18:29 UTC](https://rubytalk.org/t/n00b-reading-a-class-file-and-calling-it-at-runtime/41918/8 "2007-11-05T18:29:43Z")

</div>

Miss Elaine Eos wrote:

> In article  
> \<3736dd30711042349n7225229dw4af74110f2a52048@mail.gmail.com\>,
> 
> > only the last call to some\_method fails. Have you checked that the  
> > name of the class is correct or if you are defining your plugin class  
> > within a module?
> 
> I am NOT using "module." I guess I need to go read-up on modules --  
> Thanks! 🙂

Maybe you should post a simplified version of one of the files you are  
requiring, e.g.

class Dog  
&nbsp;&nbsp;def speak  
&nbsp;&nbsp;&nbsp;&nbsp;puts "Woof woof"  
&nbsp;&nbsp;end  
end

As this example shows:

> Sean O'halpin wrote:
> 
> # ./myplugin.rb
> 
> class MyPlugin  
> &nbsp;&nbsp;def some\_method  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "Hi from #{self}!"  
> &nbsp;&nbsp;end  
> end
> 
> ---------------
> 
> # test-require-plugin.rb
> 
> one\_plugin = "myplugin"  
> PLUG\_IN\_DIR = '.'  
> plugin\_class\_name = "MyPlugin"
> 
> require "#{PLUG\_IN\_DIR}/#{one\_plugin}"  
> plugin\_class = Object.const\_get(plugin\_class\_name).new  
> plugin\_class.some\_method
> 
> # output  
> Hi from #\<MyPlugin:0xb7c25594\>!

...you don't need to have your class inside a module to get your code to  
work. But the rest of the example showed that IF your class definition  
is inside a module, then you have to alter your syntax a little to  
retrieve the class with const\_get.

Here is another example if it helps (all files are in the current  
directory):

#plugin.rb:

class Dog  
&nbsp;&nbsp;def speak  
&nbsp;&nbsp;&nbsp;&nbsp;puts "Woof woof"  
&nbsp;&nbsp;end  
end

#my\_program.rb:

require 'plugin'

dog\_class = Object.const\_get("Dog")  
dog = dog\_class.new  
dog.speak

> **···**
>
> > sean.ohalpin@gmail.com wrote:
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

### Author: ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)
#### Post date: [5 November 2007 18:57 UTC](https://rubytalk.org/t/n00b-reading-a-class-file-and-calling-it-at-runtime/41918/9 "2007-11-05T18:57:05Z")

</div>

7stud -- wrote:

> it's only tracking const\_get through ruby's  
> extremely confusing circular inheritance:
> 
> Object \<------+  
> &nbsp;&nbsp;\> \>  
> &nbsp;&nbsp;V |  
> Module |  
> --const\_get |  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\>  
> &nbsp;&nbsp;\> \>  
> &nbsp;&nbsp;V |  
> Class ---------+

This might be a clearer diagram:

Object  
&nbsp;&nbsp;\>  
&nbsp;&nbsp;V  
Module  
--const\_get

&nbsp;&nbsp;\>  
&nbsp;&nbsp;V  
Class  
&nbsp;&nbsp;\>  
&nbsp;&nbsp;V  
Object (Object is a subclass of Class)

puts Class.kind\_of?(Object)  
--\>true

puts Object.kind\_of?(Class)  
--\>true

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

### Author: ![Gavin\_Kistner3](https://avatars.discourse-cdn.com/v4/letter/g/dfb087/32.png) [@Gavin\_Kistner3](https://rubytalk.org/u/Gavin_Kistner3)
#### Post date: [5 November 2007 19:45 UTC](https://rubytalk.org/t/n00b-reading-a-class-file-and-calling-it-at-runtime/41918/10 "2007-11-05T19:45:05Z")

</div>

For me personally, having someone write Module.const\_get versus  
Class.const\_get implies that the former will only return a Module  
instance, and the latter would only return a Class instance. By using  
Object.const\_get you are clearly stating that there is no guarantee as  
to what type of object the supplied constant points to.

> **···**
>
> On Nov 5, 11:43 am, bbxx789\_0...@yahoo.com wrote:
> 
> > 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?
> 
> > 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.

---

<div class="post-metadata">

### Author: ![Sean\_O\_Halpin](https://avatars.discourse-cdn.com/v4/letter/s/ac91a4/32.png) [@Sean\_O\_Halpin](https://rubytalk.org/u/Sean_O_Halpin)
#### Post date: [5 November 2007 22:36 UTC](https://rubytalk.org/t/n00b-reading-a-class-file-and-calling-it-at-runtime/41918/11 "2007-11-05T22:36:56Z")

</div>

It's a way of looking up constants in the scope of the top level  
binding (the special Object known as 'main').

Regards,  
Sean

P.S. Thanks for clarifying my other post 🙂

> **···**
>
> On 11/5/07, 7stud -- \<bbxx789\_05ss@yahoo.com\> wrote:
> 
> > 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?

---

<div class="post-metadata">

### Author: ![Miss\_Elaine\_Eos](https://avatars.discourse-cdn.com/v4/letter/m/76d3ee/32.png) [@Miss\_Elaine\_Eos](https://rubytalk.org/u/Miss_Elaine_Eos)
#### Post date: [6 November 2007 06:40 UTC](https://rubytalk.org/t/n00b-reading-a-class-file-and-calling-it-at-runtime/41918/12 "2007-11-06T06:40:55Z")

</div>

In article \<Misc-4BC90C.22022205112007@newsclstr03.news.prodigy.net\>,

> **···**
>
> Miss Elaine Eos \<Misc@your-pants.PlayNaked.com\> wrote:
> 
> > Ok, I tried your example, below, and it worked the same for me as for  
> > you. But when I go back to my actual code, same problem. I don't  
> > follow what's different about what your example is doing and what I'm  
> > doing. Here are some differences:
> > 
> > \* I'm running under rails. Can't see how that should matter -- this is  
> > long before any of the rendering stuff, and no DB accesses. I'm just  
> > triggering code in a controller. That's all ruby, right?
> 
> It seems that my problems had to do with leaving rails (WEBrick) running  
> and changing my controller/plug-in files. Once I quit & restarted  
> WEBrick, everything started working just fine.
> 
> Not sure why -- I though in the past that redefining objects worked  
> correctly.
> 
> Anyway, thank you much for your help!
> 
> --  
> Please take off your pants or I won't read your e-mail.  
> I will not, no matter how "good" the deal, patronise any business which sends  
> unsolicited commercial e-mail or that advertises in discussion newsgroups.

---

<div class="post-metadata">

### Author: ![a11](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/a11/32/8169_2.png) [@a11](https://rubytalk.org/u/a11)
#### Post date: [5 November 2007 21:20 UTC](https://rubytalk.org/t/n00b-reading-a-class-file-and-calling-it-at-runtime/41918/13 "2007-11-05T21:20:43Z")

</div>

i prefer Object.const\_get too, although for different reasons: it \*should\* be well known by ruby programmers that the 'top' context is the Object instances called 'main'. another approach, suitable for some limited situations is simply

object = eval name, Binding::Top

BEGIN{  
&nbsp;&nbsp;&nbsp;Binding::Top = binding  
}

which is less safe, of course, but works with effort for cases like

object = eval 'File::Stat', Binding::Top

which, for some reason, routinely confounds people on the list...

2 cts.

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

> **···**
>
> On Nov 5, 2007, at 12:45 PM, Phrogz wrote:
> 
> > For me personally, having someone write Module.const\_get versus  
> > Class.const\_get implies that the former will only return a Module  
> > instance, and the latter would only return a Class instance. By using  
> > Object.const\_get you are clearly stating that there is no guarantee as  
> > to what type of object the supplied constant points to.
> 
> --  
> it is not enough to be compassionate. you must act.  
> h.h. the 14th dalai lama

---

<div class="post-metadata">

### Author: ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)
#### Post date: [5 November 2007 23:48 UTC](https://rubytalk.org/t/n00b-reading-a-class-file-and-calling-it-at-runtime/41918/14 "2007-11-05T23:48:55Z")

</div>

Sean O'halpin wrote:

> > 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?
> 
> It's a way of looking up constants in the scope of the top level  
> binding (the special Object known as 'main').

... so are Module.const\_get and Class.const\_get:

class Dog  
end

module Stuff  
&nbsp;&nbsp;Greeting = "hello"  
end

MyConst = 10

puts Module.const\_get("Dog").new  
puts Module.const\_get("Stuff")::Greeting  
puts Module.const\_get("MyConst")  
puts  
puts Class.const\_get("Dog").new  
puts Class.const\_get("Stuff")::Greeting  
puts Class.const\_get("MyConst")

--output:--  
#\<Dog:0x251c0\>  
hello  
10

#\<Dog:0x25148\>  
hello  
10

> **···**
>
> > On 11/5/07, 7stud -- \<bbxx789\_05ss@yahoo.com\> wrote:
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

### Author: ![Sean\_O\_Halpin](https://avatars.discourse-cdn.com/v4/letter/s/ac91a4/32.png) [@Sean\_O\_Halpin](https://rubytalk.org/u/Sean_O_Halpin)
#### Post date: [5 November 2007 22:39 UTC](https://rubytalk.org/t/n00b-reading-a-class-file-and-calling-it-at-runtime/41918/15 "2007-11-05T22:39:34Z")

</div>

You can also use the predefined constant TOPLEVEL\_BINDING, e.g.

&nbsp;&nbsp;object = eval 'File::Stat', TOPLEVEL\_BINDING

Regards,  
Sean

> **···**
>
> On 11/5/07, ara.t.howard \<ara.t.howard@gmail.com\> wrote:
> 
> > another approach, suitable for  
> > some limited situations is simply
> > 
> > object = eval name, Binding::Top
> > 
> > BEGIN{  
> > &nbsp;&nbsp;&nbsp;Binding::Top = binding  
> > }
> > 
> > which is less safe, of course, but works with effort for cases like
> > 
> > object = eval 'File::Stat', Binding::Top

---

<div class="post-metadata">

### Author: ![Morton\_Goldberg](https://avatars.discourse-cdn.com/v4/letter/m/7ea924/32.png) [@Morton\_Goldberg](https://rubytalk.org/u/Morton_Goldberg)
#### Post date: [5 November 2007 22:50 UTC](https://rubytalk.org/t/n00b-reading-a-class-file-and-calling-it-at-runtime/41918/16 "2007-11-05T22:50:27Z")

</div>

You probably have a good reason for writing the above rather than the simpler

&nbsp;&nbsp;&nbsp;&nbsp;eval('File::Stat', TOPLEVEL\_BINDING) # =\> File::Stat

but I can't figure out what it is. So please explain.

Regards, Morton

> **···**
>
> On Nov 5, 2007, at 4:20 PM, ara.t.howard wrote:
> 
> > object = eval name, Binding::Top
> > 
> > BEGIN {  
> > &nbsp;&nbsp;Binding::Top = binding  
> > }
> > 
> > which is less safe, of course, but works with effort for cases like
> > 
> > object = eval 'File::Stat', Binding::Top
> > 
> > which, for some reason, routinely confounds people on the list...
> > 
> > 2 cts.

---

<div class="post-metadata">

### Author: ![Sean\_O\_Halpin](https://avatars.discourse-cdn.com/v4/letter/s/ac91a4/32.png) [@Sean\_O\_Halpin](https://rubytalk.org/u/Sean_O_Halpin)
#### Post date: [6 November 2007 07:17 UTC](https://rubytalk.org/t/n00b-reading-a-class-file-and-calling-it-at-runtime/41918/17 "2007-11-06T07:17:18Z")

</div>

Hi,

This illustrates what I mean:

Greeting = "goodbye"

class Module  
Greeting = "hello"  
end

class Class  
Greeting = "hi!"  
end

puts Module.const\_get("Greeting")  
puts Class.const\_get("Greeting")  
puts Object.const\_get("Greeting")  
puts eval( "Greeting", TOPLEVEL\_BINDING ) # for Ara 😉

--output--  
hello  
hi!  
goodbye  
goodbye

Regards,  
Sean

> **···**
>
> On 11/5/07, 7stud -- \<bbxx789\_05ss@yahoo.com\> wrote:
> 
> > \>\> I have a question about using Object as the receiver in the above line.  
> > \>\> Is there any specific reason you are using Object?  
> > \>  
> > \> It's a way of looking up constants in the scope of the top level  
> > \> binding (the special Object known as 'main').  
> > \>
> > 
> > ... so are Module.const\_get and Class.const\_get:
> > 
> > class Dog  
> > end
> > 
> > module Stuff  
> > &nbsp;&nbsp;Greeting = "hello"  
> > end
> > 
> > MyConst = 10
> > 
> > puts Module.const\_get("Dog").new  
> > puts Module.const\_get("Stuff")::Greeting  
> > puts Module.const\_get("MyConst")  
> > puts  
> > puts Class.const\_get("Dog").new  
> > puts Class.const\_get("Stuff")::Greeting  
> > puts Class.const\_get("MyConst")
> > 
> > --output:--  
> > #\<Dog:0x251c0\>  
> > hello  
> > 10
> > 
> > #\<Dog:0x25148\>  
> > hello  
> > 10

---

<div class="post-metadata">

### Author: ![a11](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/a11/32/8169_2.png) [@a11](https://rubytalk.org/u/a11)
#### Post date: [5 November 2007 23:26 UTC](https://rubytalk.org/t/n00b-reading-a-class-file-and-calling-it-at-runtime/41918/18 "2007-11-05T23:26:29Z")

</div>

a) always forgetting what the constant is named  
b) begin too lazy to look it when i was posting

TOPLEVEL\_BINDING is, of course, the better way to go! 😉

sorry for any confusion caused by my laziness.

ps. i \*do\* wish the top level binding was part of the Binding class, Binding.top, or something, because i can never remember where it lives...

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

> **···**
>
> On Nov 5, 2007, at 3:50 PM, Morton Goldberg wrote:
> 
> > You probably have a good reason for writing the above rather than the simpler
> > 
> > &nbsp;&nbsp;&nbsp;eval('File::Stat', TOPLEVEL\_BINDING) # =\> File::Stat
> > 
> > but I can't figure out what it is. So please explain.
> 
> --  
> we can deny everything, except that we have the possibility of being better. simply reflect on that.  
> h.h. the 14th dalai lama

---

<div class="post-metadata">

### Author: ![a11](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/a11/32/8169_2.png) [@a11](https://rubytalk.org/u/a11)
#### Post date: [5 November 2007 23:27 UTC](https://rubytalk.org/t/n00b-reading-a-class-file-and-calling-it-at-runtime/41918/19 "2007-11-05T23:27:04Z")

</div>

laziness 😉 sorry.

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

> **···**
>
> On Nov 5, 2007, at 3:50 PM, Morton Goldberg wrote:
> 
> > but I can't figure out what it is. So please explain.
> 
> --  
> we can deny everything, except that we have the possibility of being better. simply reflect on that.  
> h.h. the 14th dalai lama
