Wrapping ENV

I just wrote a little piece of code. Is it useful to anyone but
me? And/or do you see a more elegant way to do it, or any problems
with it?

I get tired of writing:

ENV[“this”] = x
y = ENV[“that”]

I’d rather write:

env.this = x
y = env.that

(Saving me brackets, quotes, and shift key.)

Here’s the code:

module EnvWrap
ENV.to_hash.each do |name,value|
newname = name.dup.downcase
define_method(newname) { ENV[name] }
define_method(newname+"=") {|val| ENV[name] = val}
end
end

env = Object.new.extend EnvWrap

p env.path
env.path = "wocka"
p env.path

p ENV[‘PATH’]

Cheers,
Hal

···


Hal Fulton
hal9000@hypermetrics.com

Yes, that is useful. Very cool.

Hmm, not all env names are caps.

How does one access ENV[“fred”] vs ENV[“Fred”] vs ENV[“FRED”].

···

On Friday, 22 August 2003 at 5:03:51 +0900, Hal E. Fulton wrote:

I just wrote a little piece of code. Is it useful to anyone but
me? And/or do you see a more elegant way to do it, or any problems
with it?

(Saving me brackets, quotes, and shift key.)

Here’s the code:

module EnvWrap
ENV.to_hash.each do |name,value|
newname = name.dup.downcase
define_method(newname) { ENV[name] }
define_method(newname+“=”) {|val| ENV[name] = val}
end
end

env = Object.new.extend EnvWrap

p env.path
env.path = “wocka”
p env.path

p ENV[‘PATH’]


Jim Freeze

Jenkinson’s Law:
It won’t work.

Hal E. Fulton wrote:

I just wrote a little piece of code. Is it useful to anyone but
me? And/or do you see a more elegant way to do it, or any problems
with it?

I get tired of writing:

ENV[“this”] = x
y = ENV[“that”]

I’d rather write:

env.this = x
y = env.that

p env.path
env.path = “wocka”
p env.path

p ENV[‘PATH’]

Cheers,
Hal

You must have been using Javascript lately;-)

/Christoph

Hal E. Fulton wrote:

I just wrote a little piece of code. Is it useful to anyone but
me? And/or do you see a more elegant way to do it, or any problems
with it?

I get tired of writing:

ENV[“this”] = x
y = ENV[“that”]

I’d rather write:

env.this = x
y = env.that

(Saving me brackets, quotes, and shift key.)

Here’s the code:

module EnvWrap
ENV.to_hash.each do |name,value|
newname = name.dup.downcase
define_method(newname) { ENV[name] }
define_method(newname+“=”) {|val| ENV[name] = val}
end
end

env = Object.new.extend EnvWrap

p env.path
env.path = “wocka”
p env.path

p ENV[‘PATH’]

Cheers,
Hal

That’s pretty useful! Why not generalize it though:

class Record
def initialize(source)
source.to_hash.each do |name, value|
method_name = name.downcase
define_method(method_name) { source[name] }
define_method(method_name + “=”) { |value| source[name] = value }
end
end
end

env = Record.new(ENV)

p env.path
env.path = “wocka”
p env.path

p ENV[‘PATH’]

Sean O'Dell

I don't like it. I have some environment variables with names like
"My-Env-Data". This is not a valid method name.

And i don't now what is the real benefit of your code ?
ENV['foo'] is in my opinion much more readable. And i never count the
time for typing my program. I write it once but read it many times.

I just wrote a little piece of code. Is it useful to anyone but
me? And/or do you see a more elegant way to do it, or any problems
with it?

Mentioned in other posts, it assumes that environmental variables are
uppercase. No biggie, most of them are. But I can’t resist throwing code
out, so what about something like this:

class WrapHash
def initialize(hash)
@hash = hash
end
def method_missing(symbol, *args)
str = symbol.id2name
setter = str.sub!(/=$/, “”)
var = [str, str.upcase, str.downcase].detect { |key| @hash.include?(key) }
if setter
@hash[var] = *args
else
@hash[var]
end
end
end

e = WrapHash.new(ENV)
=> #<WrapHash:0x402b5fe4 @hash={ snipped big environment inspect }>
e.pager
=> “less -irs”
e.PaGeR
=> “less -irs”
e.pager = “more”
=> “more”
ENV[“pager”]
=> nil
ENV[“PAGER”]
=> “more”

Jason Creighton

···

On Fri, 22 Aug 2003 05:03:51 +0900 “Hal E. Fulton” hal9000@hypermetrics.com wrote:

Quite right.

In the piece of shell code I’m porting, they are.

I think that since we’re invoking methods explicitly,
it should still work if you remove the downcase stuff. It
shouldn’t get confused thinking caps are constants. Probably
won’t even have to add parens. I’ll test soon.

Hal

···

----- Original Message -----
From: “Jim Freeze” jim@freeze.org
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Thursday, August 21, 2003 3:32 PM
Subject: Re: Wrapping ENV

Hmm, not all env names are caps.

How does one access ENV[“fred”] vs ENV[“Fred”] vs ENV[“FRED”].

You must have been using Javascript lately;-)

Actually, as little as possible. :slight_smile:

FYI, there are some issues with my code:

  1. It pretends all env vars are all upper case
  2. It doesn’t allow adding new vars
  3. It doesn’t allow for nonexistent vars

David Alan Black had a fix that handled (2) and (3). And another
private email asked a question that David’s email almost answered.

Hmm, what a world… off-topic goes to go to the list, and on-topic
replies are sent to me privately… :wink:

What times these are, when passing ruffians may with impunity
say “Ni!” to old ladies on the street…

Cheers,
Hal

···

----- Original Message -----
From: “Christoph R.” chr_news@gmx.net
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Thursday, August 21, 2003 3:35 PM
Subject: Re: Wrapping ENV


Hal Fulton
hal9000@hypermetrics.com

I don’t like it. I have some environment variables with names like
“My-Env-Data”. This is not a valid method name.

It’s useless for you then.

And i don’t now what is the real benefit of your code ?
ENV[‘foo’] is in my opinion much more readable. And i never count the
time for typing my program. I write it once but read it many times.

It’s even more useless for you, then. :slight_smile:

It’s a question of taste. I do find it more readable as well as
easier to type. I’d rather simulate the environment as a struct
than a hash.

But of course there are significant problems with it.

Hal

···

----- Original Message -----
From: “Lothar Scholz” mailinglists@scriptolutions.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org; hal9000@hypermetrics.com
Cc: “(ruby-talk ML)” ruby-talk@ruby-lang.org
Sent: Thursday, August 21, 2003 7:13 PM
Subject: Re: Wrapping ENV

Lothar Scholz wrote:

And i don’t now what is the real benefit of your code ?
ENV[‘foo’] is in my opinion much more readable. And i never count the
time for typing my program. I write it once but read it many times.

If you like things much more readable and you never count the time for
typing I would suggest this code:

THE_ENVIRONMENT_VARIABLES = ENV

:slight_smile:

Why not make use of method_missing instead of defining methods for each
existing variable? The following code will work for existing variables as
well as let you add new ones.

class MyEnv
def method_missing(symbol, *args)
str = symbol.id2name

    if args.length == 0
        ENV[str]
    else
        ENV[str] = args[0].to_s
    end
end

end

e = MyEnv.new
e.foo 23
e.foo => 23
e.path(e.path + “/tmp”)
e.path => “…:/tmp”

···

On Fri, 22 Aug 2003 07:15:33 +0900, “Sean O’Dell” <sean@cSePlsoAfMt.com[REMOVE_THE_SPAM]> wrote:

That’s pretty useful! Why not generalize it though:

class Record
def initialize(source)
source.to_hash.each do |name, value|
method_name = name.downcase
define_method(method_name) { source[name] }


Dean saor, dean saor an spiorad. Is seinn d’orain beo.

I don’t like it. I have some environment variables with names like
“My-Env-Data”. This is not a valid method name.

And i don’t now what is the real benefit of your code ?

Well it increases the signal to noise ratio of course :slight_smile:

I find it very useful as a basis for some other uses (specialy like it
was later generalized). I haven’t yet seen anything about the remote ruby
project, but I came into ruby
willing to do domething similar (if the remote ruby project doesn’t already
do what I want, that is).
it’s very useful to “wrap” other objects, may it be a remote object, an ole
object or whatever wrapped object could make sense in your environment. you
make a generic base class which connects to the wrapped object and queries
it,
and instead of making its methods available through generic calls
(exec_method(method_name, blablabla)) you can
write wrapped_object.method_name(blabla), and now, the same with properties
and array properties should be possible, I’m still learning how to just
print hello world, but I was meaning to get in this direction as soon as
possible (and see if it’s even possible to do) this will save me a lot of
research :slight_smile:

ENV[‘foo’] is in my opinion much more readable. And i never count the
time for typing my program. I write it once but read it many times.

That’s a good point, I don’t think I’d use this code for this particular
task either, but the code is still useful.

Cheers…

Olivier.

If you like things much more readable and you never count the time for
typing I would suggest this code:

THE_ENVIRONMENT_VARIABLES = ENV

verbosity != readable.

In fact your suggestion is much less readable. The normal mind can put
his focus on 7 items at a time. where ‘_’ separated parts of the identifier
counts as one. If this variable is used in a statement is is very
likely that it will increase the number of tokens over this magic
number.

Lothar Scholz wrote:

If you like things much more readable and you never count the time for
typing I would suggest this code:

THE_ENVIRONMENT_VARIABLES = ENV

verbosity != readable.

In fact your suggestion is much less readable. The normal mind can put
his focus on 7 items at a time. where ‘_’ separated parts of the identifier
counts as one. If this variable is used in a statement is is very
likely that it will increase the number of tokens over this magic
number.

It was a joke. You snipped off the :)!! Dang, misquoted again.