Ok, recently started “trying” to learn this wonderful language, but am
experiencing major problems with a section of code that I think “should”
work… and my brain is so convinced that I did it correctly that I can’t
see some obvious error. Ive isolated the problem to this single section
of code… what do you all think?

#This section reads data files and saves odd lines as the key, even
lines as the value for a hash.

each_slice(2) is taking each pair of lines in the file, and passing
them to the block. So, [line1, line2], then [line3, line4], etc, etc.
You should probably also have a check in there to make sure the file
has an even number of lines.

You can also use File.exists?() to check for the presence of a
directory.
abort “Oh no!” unless File.exists?(“C:/Program
Files/something/data_dir”)

Great answers guys! Thanks for the help… surprisingly a part of what
was throwing me off was not having food in the tummy apparently, as when
that problem was fixed I immediately realized part of my problem was not
knowing what “file.lineno” was… and all I can say is “duh”. Makes alot
of sense now. and good trick with the % very crafty.

Thanks again all… so that you know your efforts wern’t in vain I think
I learned a thing or two. The rough code (still needing debugging about
as much as I need sleep) is below. I have a high level of confidence
that I will find the typo or missing end to make it work when I wake up
in the morning.

Even better would be to use File.directory? to test for presence of this
directory. And it would be even better if the absence of the directory
would be handled properly. As far as I can see at the moment
script_data_dir simply remains unset and thus the current working
directory
is used.

{} is equivalent to Hash.new

Use { |x| … } block syntax when the block has only one simple
statement (the each_slice part)

Don’t use blocks or procs unless you need them (i.e. want to encapsulate
a
piece of code and refer it from a variable).

Don’t assign a global variable as a side effect from your proc / method
if
you also return it. There’s especially the problem of your code that
you
only get the last hash because all others are lost (the global is
overwritten all the time).

What do you use the second parameter for? The only reasonable value
here
seems to be ‘*.dat’. Also you’re trying to remove the suffix “.dat” and
reappend it again. Seems a bit strange to me.

Yeah I was being silly there. I actually intended to remove the
extension from the filename, thanks for the correction.

file.open should be File.open ( actually, you can leave off File
too, so it is just open(…). See Kernel#open ).

file. basename should be File.basename

Also it’s unnecessary since Dir[] always returns basenames only.

Thank you both. seems that I was misunderstanding how Dir[] worked
exactly. Ill play with that some more.

{} is equivalent to Hash.new

Use { |x| … } block syntax when the block has only one simple
statement (the each_slice part)

Don’t use blocks or procs unless you need them (i.e. want to encapsulate
a
piece of code and refer it from a variable).

I am not exactly sure how you mean by this but I had thought I was doin
it with
“build_hash.call(File.basename(name_of_file))”

Don’t assign a global variable as a side effect from your proc / method
if
you also return it. There’s especially the problem of your code that
you
only get the last hash because all others are lost (the global is
overwritten all the time).

What I had thought this would be doing is to take its input (aka.
Test1.dat or Test2.dat) and make Test1_hash, or Test2_hash and then
convert those to $Test1_hash or $Test2_hash for use elsewhere. am I
experiencing a gross concept error here?

Use { |x| … } block syntax when the block has only one simple
statement (the each_slice part)

Actually the first conditional will have elsifs once I get the code
working, hence the reason for writing it that way. I intend to make it
viable for unix platform for the same program aswell, in which case I
will have to look elsewhere for the appropriate directory