Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ahoward@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================
Thank you! This is clear and still concise. The other responses seem
to gravitate towards Perl-like one-liners. But this defeats the
purpose of writing in Ruby—to avoid Perl obfuscations. The above is
much more readable and also much more idiomatic than many of the
others. This should go into athe Ruby Cookbook (if it were still up).
h = {}
File.foreach(“fred.ini”) do |line|
key, value = line.chomp.split(“=”)
h[key] = value
end
Ross
Thank you! This is clear and still concise. The other responses
seem
to gravitate towards Perl-like one-liners. But this defeats the
purpose of writing in Ruby—to avoid Perl obfuscations. The above
is
much more readable and also much more idiomatic than many of the
others. This should go into athe Ruby Cookbook (if it were still
up).
Well said; I found this to be the best balanced between rubyish and
still readable.
···
=====
Yahoo IM: michael_s_campbell
Do you Yahoo!?
Yahoo! Mail Plus Powerful. Affordable. Sign up now.
h = {}
File.foreach(“fred.ini”) do |line|
key, value = line.chomp.split(“=”)
h[key] = value
end
Ross
Thank you! This is clear and still concise. The other responses seem
to gravitate towards Perl-like one-liners. But this defeats the
purpose of writing in Ruby—to avoid Perl obfuscations. The above is
much more readable and also much more idiomatic than many of the
others. This should go into athe Ruby Cookbook (if it were still up).
Uh oh, I think I might be an obfuscation suspect But please – I
must insist that my obfuscations be denounced as Ruby obfuscations,
not Perl ones Perl is a red herring here; Perl style isn’t
necessarily a point of reference at all, one way or the other, when
one decides to use Ruby or to do specific things in Ruby.
I personally put reading a config file in a kind of utilitarian,
“let’s just do this and move on” category – and those types of things
I do like to trim down to near-minimum code. It’s my impression that
that can actually add to the overall clarity of a program file. But a
lot of this is in the eye of the beholder. Ross’s code certainly
looks fine to me.
Idiomatic-ness is of course hard to measure. But I definitely like
the Hash[*array] idiom. It’s a technique that would be more familiar
and would probably seem more idiomatic if it were used more often, and
would be used more often if it seemed more familiar… so someone’s
got to break the cycle
What’s the best ruby idiom for the following Perl:
open(EMAIL, "<$EMAIL_FILE") or die "Failed to open $EMAIL_FILE";
my %hash = map {chomp; split /\=/} (<EMAIL>);
close (EMAIL);
(the above reads a file containing text like
key1=value1
key2=value2
into a hash.
So far I have:
h = {}
f = File.open(“fred.ini”, “r”)
f.each_line{|l| s = l.chomp.split(“=”); h[s[0]] = s[1]}
f.close
h = {}
File.foreach(“fred.ini”) do |line|
key, value = line.chomp.split(“=”)
h[key] = value
end
Ross
Thank you! This is clear and still concise. The other responses seem
to gravitate towards Perl-like one-liners. But this defeats the
purpose of writing in Ruby—to avoid Perl obfuscations. The above is
much more readable and also much more idiomatic than many of the
others. This should go into athe Ruby Cookbook (if it were still up).
I have to come clean. I only did this implementation because I could not
for the life of me figure out how to get Hash update working with an
array (as per many of the other solutions). I looked up my Pickaxe and
Ruby in a Nutshell and still could not work it out. So it was only dumb
ignorance that left me with what I had.
But on reflection and given your appreciation I am happier with what I
did put up.
h = Hash[*File.new(“fred.ini”).read.split(/=|\n/)]
I like the above, but wondered it it leaves the file open?
The file will be closed when it will be garbage collected.
You again!!! ;-D
Hehe…
Intriuging. It’s indeed good to learn GC will cause the file to
be closed (makes sense.)
I don’t know if I worry too much, but that does set off my potential-
danger-alarm meter a bit, depending on conceivable circumstances:
for instance, if the above were in a loop. Would it be possible to
run out of file handles before GC was called?
If so, I would personally find myself resistant to adopting the
idiom of letting GC close file handles, as I would view the
relationship between the opener of the handles, and GC, as a
potential race condition lurking in my code. . . . But it could be
I worry too much.
The file will be closed when it will be garbage collected.
I don’t know if I worry too much, but that does set off my potential-
danger-alarm meter a bit, depending on conceivable circumstances:
for instance, if the above were in a loop. Would it be possible to
run out of file handles before GC was called?
Nope - the interpreter checks and runs GC if it runs out of file handles:
file = fopen(fname, mode);
if (!file) {
if (errno == EMFILE || errno == ENFILE) {
rb_gc();
file = fopen(fname, mode);
}
if (!file) {
rb_sys_fail(fname);
}
}
for instance, if the above were in a loop. Would it be possible to
run out of file handles before GC was called?
no,
If so, I would personally find myself resistant to adopting the
idiom of letting GC close file handles, as I would view the
relationship between the opener of the handles, and GC, as a
potential race condition lurking in my code. . . . But it could be
I worry too much.
Well, you must know that some persons like me are stupid and forget to
close the files and I frequently write
pigeon% cat b.rb
#!/usr/bin/ruby
require 'bz2'
a = File.new("aa", "w")
bz2 = BZ2::Writer.new(a)
12.times {|i| bz2.puts i }
pigeon%