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.
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.
“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?
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. 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:
“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.
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.
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.
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?
“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
matz.
···
In message “Re: ENV.clear” on 03/01/07, Jeremy Henty jeremy@chaos.org.uk writes: