ENV.clear

I tried
ENV.clear

But it doesn’t work, with the error message:
…/env.rb:56: undefined method `clear’ for #Object:0x401d0d28 (NameError)

I thought ENV is a Hash, so clear is defined. However, when I tried:
puts ENV.type

it turns out ENV is only an Object.

However, the Hash function delete(key) is defined for ENV. That is very strange.

Can someone experienced tell me what ENV is?

Thanks a lot!

Hi,

···

In message “ENV.clear” on 02/12/31, TOTO zhoujing@comp.nus.edu.sg writes:

ENV.clear

But it doesn’t work, with the error message:
…/env.rb:56: undefined method `clear’ for #Object:0x401d0d28 (NameError)

I thought ENV is a Hash, so clear is defined. However, when I tried:
puts ENV.type

it turns out ENV is only an Object.

However, the Hash function delete(key) is defined for ENV. That is very strange.

Can someone experienced tell me what ENV is?

ENV is an object that provides Hash-like singleton methods to work as
if it’s a String-key hash.

						matz.

matz@ruby-lang.org (Yukihiro Matsumoto) wrote in message news:1041323021.224559.4437.nullmailer@picachu.netlab.jp

Hi,

ENV.clear

But it doesn’t work, with the error message:
…/env.rb:56: undefined method `clear’ for #Object:0x401d0d28 (NameError)

I thought ENV is a Hash, so clear is defined. However, when I tried:
puts ENV.type

it turns out ENV is only an Object.

However, the Hash function delete(key) is defined for ENV. That is very strange.

Can someone experienced tell me what ENV is?

ENV is an object that provides Hash-like singleton methods to work as
if it’s a String-key hash.

  					matz.

Thanks, Matz.

In that case, I guess I have to copy the content of ENV to a real Hash. Am I right?

···

In message “ENV.clear” > on 02/12/31, TOTO zhoujing@comp.nus.edu.sg writes:

This works (hint: muck around in irb):

h = {}.update(ENV)

Pretty nice, eh?

Cheers,
Gavin

···

On Thursday, January 2, 2003, 2:27:10 PM, TOTO wrote:

In that case, I guess I have to copy the content of ENV to a real Hash. Am I right?

Hi,

···

In message “Re: ENV.clear” on 03/01/02, TOTO zhoujing@comp.nus.edu.sg writes:

ENV is an object that provides Hash-like singleton methods to work as
if it’s a String-key hash.

In that case, I guess I have to copy the content of ENV to a real Hash. Am I right?

It depends on what you want to do after copying the content of ENV.
If you want to remove all environment variables, copying will not
work. To clear environment variables, try “ENV.delete_if{true}”.

“clear” is not supported by ENV, just because I considered removing
all environment variables might not be intentional.

						matz.

Gavin Sinclair gsinclair@soyabean.com.au wrote in message news:9095791290.20030102143314@soyabean.com.au

In that case, I guess I have to copy the content of ENV to a real Hash. Am I right?

This works (hint: muck around in irb):

h = {}.update(ENV)

Pretty nice, eh?

Yes. It is.

The update method will return a copy of the new Hash or a copy of the
reference of the new Hash? How did you find out? I couldn’t find it in
the Ruby reference.

To be honest, I have a stupid thought originally, before I know I can
use update method. I think I should implement an insert method for
class Hash to do the same thing. And then I find that it might be
impossible to do it with Ruby itself, because the class Hash is
implemented in c.

···

On Thursday, January 2, 2003, 2:27:10 PM, TOTO wrote:

It depends on what you want to do after copying the content of ENV.
If you want to remove all environment variables, copying will not
work. To clear environment variables, try “ENV.delete_if{true}”.

“clear” is not supported by ENV, just because I considered removing
all environment variables might not be intentional.

  					matz.

Right.

In fact, I am trying to make use of ENV to implement a clone program
of the UNIX env, http://figaro.ddns.comp.nus.edu.sg/~zhoujing/codes.html
… When implementing the -i option, I need to remove all environment
variables.

[…]

This works (hint: muck around in irb):

h = {}.update(ENV)

Pretty nice, eh?

There’s also ENV.to_hash, which is a bit more direct.

		Reimer Behrends
···

Gavin Sinclair (gsinclair@soyabean.com.au) wrote:

In article 1041481251.819565.3200.nullmailer@picachu.netlab.jp,
Yukihiro Matsumoto wrote:

“clear” is not supported by ENV, just because I considered removing
all environment variables might not be intentional.

Why? When writing Perl scripts in a security-conscious way I often
deleted all environment variables, then set a chosen few (PATH,
typically) to safe values. Is this an unusual thing to want to do?
Or is there a better way to do this sort of thing in Ruby?

Regards,

Jeremy Henty

The update method will return a copy of the new Hash or a copy of the
reference of the new Hash? How did you find out? I couldn’t find it in
the Ruby reference.

‘ri’ - the Ruby programmer’s friend. You need it. :slight_smile: It’s a
destructive method. Look for ‘ri’ in the RAA.

To be honest, I have a stupid thought originally, before I know I can
use update method. I think I should implement an insert method for
class Hash to do the same thing. And then I find that it might be
impossible to do it with Ruby itself, because the class Hash is
implemented in c.

Nothing is impossible. You can add methods to the Hash class.
Generally the method you add will be implemented in terms of the
existing methods. If #update didn’t exist, you could add it thus
(untested):

class Hash
def update(other)
other.each do |key, value|
self[key] = value
end
end
end

Gavin

···

On Friday, January 3, 2003, 12:49:27 AM, TOTO wrote:

In article slrnb1blu8.ftv.jeremy@ganglion.demon.co.uk, Jeremy Henty
(ie. me) wrote:

In article 1041481251.819565.3200.nullmailer@picachu.netlab.jp,
Yukihiro Matsumoto wrote:

“clear” is not supported by ENV, just because I considered removing
all environment variables might not be intentional.

Why? When writing Perl scripts in a security-conscious way I often
deleted all environment variables, then set a chosen few (PATH,
typically) to safe values. Is this an unusual thing to want to do?

Thinking about it further, in this case I really want ENV to respond
to “replace” rather than “clear”. Currently (ie. in 1.6.8) it does
not. I would argue that ENV.replace is much less likely to be
unintentional than ENV.clear because although one rarely wants to
delete the environment entirely (eg. do you ever want an empty
PATH?) it is often sensible to reset it to some fixed value.

Convinced?

Regards,

Jeremy Henty

Thinking about it further, in this case I really want ENV to
respond
to “replace” rather than “clear”. Currently (ie. in 1.6.8) it does
not. I would argue that ENV.replace is much less likely to be
unintentional than ENV.clear because although one rarely wants to
delete the environment entirely (eg. do you ever want an empty
PATH?) it is often sensible to reset it to some fixed value.

Convinced?

I’m not. Well, actually I’m not convinced that #clear is
unintentional. The decision to not even have it in the class
somewhat astounded me.

I make a lot of typos and thinkos, but typing “clear” when I meant to
type or think something else I can’t say I’ve EVER done.

···

=====

Yahoo IM: michael_s_campbell


Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.

Hi,

Thinking about it further, in this case I really want ENV to respond
to “replace” rather than “clear”. Currently (ie. in 1.6.8) it does
not. I would argue that ENV.replace is much less likely to be
unintentional than ENV.clear because although one rarely wants to
delete the environment entirely (eg. do you ever want an empty
PATH?) it is often sensible to reset it to some fixed value.

ENV lacks the following methods:

clear, replace, update, shift, invert
default_proc, default, defalt=

the latter three (deal default values) is meaningless.
“clear”, “shift” and “update” are useless (in my opinion) but may be
harmless to have.

“replace” and “update” (they are aliases) are good to have. I will
add them later.

						matz.
···

In message “Re: ENV.clear” on 03/01/04, Jeremy Henty jeremy@chaos.org.uk writes:

In article 1041841434.749629.28285.nullmailer@picachu.netlab.jp,
Yukihiro Matsumoto wrote:

ENV lacks the following methods:

clear, replace, update, shift, invert
default_proc, default, defalt=

the latter three (deal default values) is meaningless.

Fair enough.

“clear”, “shift” and “update” are useless (in my opinion) but may be
^^^^^^ should be “invert”?
harmless to have.

“replace” and “update” (they are aliases)

??? The pickaxe book says that “replace” deletes keys in the receiver
that are not in the argument, whereas “update” does not, so they are
not the same. Surely ENV should behave this way too?

… are good to have. I will add them later.

Thank you!

Jeremy Henty

Hi,

“clear”, “shift” and “update” are useless (in my opinion) but may be
^^^^^^ should be “invert”?

Oops, Yes.

“replace” and “update” (they are aliases)

??? The pickaxe book says that “replace” deletes keys in the receiver
that are not in the argument, whereas “update” does not, so they are
not the same. Surely ENV should behave this way too?

They are not aliases. I guess I was screwed up at the time.
It’s not unusual for me :wink:

						matz.
···

In message “Re: ENV.clear” on 03/01/07, Jeremy Henty jeremy@chaos.org.uk writes: