Wrapping ENV

H

Hal E. Fulton

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
 
C

Christoph R.

Hal said:
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
 
J

Jim Freeze

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']

Hmm, not all env names are caps.

How does one access ENV["fred"] vs ENV["Fred"] vs ENV["FRED"].
 
S

Sean O'Dell

Hal said:
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
 
L

Lothar Scholz

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.
 
H

Hal E. Fulton

----- Original Message -----
From: "Lothar Scholz" <[email protected]>
To: "ruby-talk ML" <[email protected]>; <[email protected]>
Cc: "(ruby-talk ML)" <[email protected]>
Sent: Thursday, August 21, 2003 7:13 PM
Subject: Re: Wrapping ENV

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. :)

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
 
M

mgarriss

Lothar said:
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

:)
 
L

Lothar Scholz

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.
 
J

Joey Gibson

On Fri, 22 Aug 2003 07:15:33 +0900, "Sean O'Dell"

||| 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] }

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"
 
O

Olivier Nenert

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 :)

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 :)
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.
 
J

Jason Creighton

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)
=> # said:
e.pager => "less -irs"
e.PaGeR => "less -irs"
e.pager = "more" => "more"
ENV["pager"] => nil
ENV["PAGER"]
=> "more"

Jason Creighton
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,109
Messages
2,570,671
Members
47,262
Latest member
EffiePju4

Latest Threads

Top