diff --git a/hash.c b/hash.c index 8af5be688e..be4fc216d4 100644 --- a/hash.c +++ b/hash.c @@ -5699,6 +5699,20 @@ env_reject(void) return rb_hash_delete_if(env_to_hash()); } +/* + * call-seq: + * ENV.freeze -> raises TypeError + * + * Ruby does not allow ENV to be frozen, so calling ENV.freeze + * raises TypeError. + */ +static VALUE +env_freeze(VALUE self) +{ + rb_raise(rb_eTypeError, "cannot freeze ENV"); + return self; /* Not reached */ +} + /* * call-seq: * ENV.shift -> Array or nil @@ -6058,6 +6072,7 @@ Init_Hash(void) rb_define_singleton_method(envtbl, "filter", env_select, 0); rb_define_singleton_method(envtbl, "filter!", env_select_bang, 0); rb_define_singleton_method(envtbl, "shift", env_shift, 0); + rb_define_singleton_method(envtbl, "freeze", env_freeze, 0); rb_define_singleton_method(envtbl, "invert", env_invert, 0); rb_define_singleton_method(envtbl, "replace", env_replace, 1); rb_define_singleton_method(envtbl, "update", env_update, 1); diff --git a/test/ruby/test_env.rb b/test/ruby/test_env.rb index 6343642ac1..e8ae762d03 100644 --- a/test/ruby/test_env.rb +++ b/test/ruby/test_env.rb @@ -496,6 +496,10 @@ class TestEnv < Test::Unit::TestCase end end + def test_frozen + assert_raise(TypeError) { ENV.freeze } + end + def test_frozen ENV[PATH_ENV] = "/" ENV.each do |k, v|