[Ironruby-core] Singleton methods on builtin global constants

Wayne Kelly w.kelly at qut.edu.au
Mon Feb 18 23:43:37 EST 2008

I've been looking into implementing environment variable lookup, eg ENV['RUBYOPT']  (as it is required for gems setup).

ENV is a builtin global constant. In the CRuby implementation it points to a plain old  Ruby object that has a number of singleton methods defined on it, including [] which is used for looking up a specified environment variable.

In IronRuby the RubyExecutionContext class has a method called InitializeGlobalConstants which seems to be used to initialize such global constants. It's therefore easy enough to define a new global variable called "ENV" and to initialize it to an object. But where then do we attach the 38 singleton methods to this class?

These environment methods will presumably be implemented in the IronRuby.Libraries assembly? The method attributes and automatic class init generator allows singleton methods to be automatically added to modules, but doesn't appear to support adding singleton methods to well known objects. You can't even write the initialization code manually in say InitializeGlobalConstants as the methods that are needed are from the IronRuby.Library assembly which is not referenced by the Ruby assembly.

It seems there are three choices:
1) somehow extend the attribute and automatic generator mechanism to allow singleton methods to be attached to well know objects.
2) instead of storing a plain old Ruby object in ENV, create an instance of a new ENV class (defined in the Ruby assembly?) that has instance methods rather than singleton methods (and perhaps somehow hide the existence of this class from the Ruby programmer?).
3) Manually write initialization code to attach the singleton methods - but we'd still need to work out where to place this code  to avoid cyclic dependences between the two assemblies.

Note, this is not just an issue for ENV, a number of other builtin global constants also have dozens of singleton methods attached to them and these are all yet to be implemented in IronRuby.

Cheers, Wayne.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/ironruby-core/attachments/20080219/669103dc/attachment-0001.html 

More information about the Ironruby-core mailing list