/**
 * Copyright 2007 Xue Yong Zhi
 * Distributed under the GNU General Public License 2.0
 */

package com.xruby.runtime.builtin;

import com.xruby.runtime.lang.RubyAPI;
import com.xruby.runtime.lang.RubyBlock;
import com.xruby.runtime.lang.RubyObject;
import com.xruby.runtime.lang.RubyOneArgMethod;
import com.xruby.runtime.lang.RubyRuntime;
import com.xruby.runtime.lang.RubyValue;
import com.xruby.runtime.value.ObjectFactory;
import com.xruby.runtime.value.RubyString;

class ENV_array_get extends RubyOneArgMethod {
    protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
        RubyString s = RubyTypesUtil.convertToString(arg);
        String v = System.getenv(s.toString());
        if (null == v) {
            return ObjectFactory.NIL_VALUE;
        } else {
            return ObjectFactory.createString(v);
        }
    }
}

public class ENVInitializer {
    public static void initialize() {
        RubyValue v = RubyAPI.setTopLevelConstant(new RubyObject(RubyRuntime.ObjectClass), "ENV");
        v.getSingletonClass().defineMethod("[]", new ENV_array_get());
    }
}

@@ -132,16 +132,41 @@
 tin;%0D%0A%0D%0A
+import java.util.Map;%0D%0A%0D%0A
 import c
@@ -197,16 +197,16 @@
 byAPI;%0D%0A
-
 import c
@@ -913,16 +913,574 @@
 %7D%0D%0A%7D%0D%0A%0D%0A
+class ENV_index extends RubyOneArgMethod %7B%0D%0A    protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) %7B%0D%0A        String s = RubyTypesUtil.convertToJavaString(arg);%0D%0A        Map%3CString, String%3E map = System.getenv();%0D%0A%0D%0A        if (map.containsValue(s)) %7B%0D%0A            for (String k : map.keySet()) %7B%0D%0A                String v = map.get(k);%0D%0A                if (s.equals(v)) %7B%0D%0A                    return ObjectFactory.createString(k);%0D%0A                %7D%0D%0A            %7D%0D%0A        %7D%0D%0A%0D%0A        return ObjectFactory.NIL_VALUE;%0D%0A    %7D%0D%0A%7D%0D%0A%0D%0A
 public c
@@ -1637,24 +1637,24 @@
 ), %22ENV%22);%0D%0A
-
         v.ge
@@ -1709,22 +1709,93 @@
 ay_get());%0D%0A
+        v.getSingletonClass().defineMethod(%22index%22, new ENV_index());%0D%0A
     %7D%0D%0A%7D%0D%0A

add ENV#index