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:
somehow extend the attribute and automatic generator mechanism to
allow singleton methods to be attached to well know objects.
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?).
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.
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.
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:
somehow extend the attribute and automatic generator mechanism to
allow singleton methods to be attached to well know objects.
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?).
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.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.