Wayne Kelly
2008-Feb-19 04:43 UTC
[Ironruby-core] Singleton methods on builtin global constants
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
Tomas Matousek
2008-Feb-19 07:45 UTC
[Ironruby-core] Singleton methods on builtin global constants
Interesting. We have traits for main singleton and some other singleton objects in SingletonOps. I guess ENV will be similar (say, you would declare EnvironmentSingletonOps class in the library), however these are hardcoded in the IronRuby.dll and it would be clearly better to provide an extension point here. Will look at it at let you know. Tomas From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Wayne Kelly Sent: Monday, February 18, 2008 8:44 PM To: ironruby-core at rubyforge.org Subject: [Ironruby-core] Singleton methods on builtin global constants 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/20080218/6af67a1b/attachment.html