# Singleton methods on builtin global constants

**URL:** <https://www.ruby-forum.com/t/singleton-methods-on-builtin-global-constants/129509>\
**Category:** IronRuby\
**Created:** [February 19, 2008, 5:44am UTC](https://www.ruby-forum.com/t/singleton-methods-on-builtin-global-constants/129509 "2008-02-19T05:44:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wayne\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b5ac83/32.png) [@Wayne\_K](https://www.ruby-forum.com/u/Wayne_K)\
**Post date:** [February 19, 2008, 5:44am UTC](https://www.ruby-forum.com/t/singleton-methods-on-builtin-global-constants/129509/1 "2008-02-19T05:44:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Wayne\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b5ac83/32.png) [@Wayne\_K](https://www.ruby-forum.com/u/Wayne_K)\
**Post date:** [February 19, 2008, 8:46am UTC](https://www.ruby-forum.com/t/singleton-methods-on-builtin-global-constants/129509/2 "2008-02-19T08:46:39Z")

</div>

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: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Wayne K.  
Sent: Monday, February 18, 2008 8:44 PM  
To: removed\_email\_address@domain.invalid  
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.
