Hi Giles,
I'm a new ruby convert (from primarily Perl, but also Java and PHP
myself). I'd like to offer my observation and see if other more
experienced Ruby programmers have input.
To risk sounding trite, my take on the most idiomatic Ruby data
structure would be the class. When I deal with CSVs I typically
use the CSV object in the standard library (though I've read there
may be better ways) and do something like the code below.
At the end of the day, it's a hash cloaked in object clothing. I
use the first line of the CSV to determine the names of the
keys in the hash (and hence the instance method names for the
object instance for each row).
The reason I take this approach is that it avoids locking you to
the hash syntax using or fetch/ store. Additionally, it would
allow you to subclass and have instance methods like:
def full_name
first + " " + last
end
I'm still learning ruby, so feed back on the approach is welcome.
#!/usr/bin/ruby
require 'csv'
class ObjectCsvParser
attr :attributes
def initialize(fields,values)
@attributes = Hash[
*[
(0..fields.length-1).map {
>i> [fields[i], values[i]]
}
].flatten
]
end
def method_missing(method_name)
if(@attributes[method_name])
@attributes[method_name]
else
raise NoMethodError.new(method_name)
end
end
def self.each_record(fn,&block)
fields = nil
CSV.open(fn,'r') do |line|
if(fields == nil)
fields = line.map { |l| l.downcase.to_sym }
next
end
yield ObjectCsvParser.new(fields,line)
end
end
end
# Assuming your CSV has last, first, and email as fields
ObjectCsvParser.each_record(ARGV.shift) do |record|
puts "Last: " + record.last
puts "First: " + record.first
puts "Email: " + record.email
end
Cheers!
Andy
Giles Bowkett wrote:
路路路
I came to Ruby from a scattering of Java, a tiny bit of Python, and a
ton of Perl. So the other day I wrote a simple utility script and
afterwards realized it may have been very influenced by all those
years of Perl. Specifically, it seemed to use hashes too much.
How can I cure myself of my hash addiction? 
Seriously -- I had a CSV file from an Excel spreadsheet. Each row
represented a thing, so I made a Struct for that thing. Those things
were in groups, so I hashed them by their common feature, a unique id
from the spreadsheet. But the last character in the unique id on the
spreadsheet was non-unique, and had to be preserved. Each group had to
have a dash-delimited list of those characters, relevant to that
particular group (since different groups would have different terminal
characters in their unique ids). So I hashed them again -- and that's
what seemed like overkill.
What's the most idiomatic Ruby data structure? Would another Struct
have been more maintainable? The code was perfectly functional, and
it's pretty readable, but you can read it and go, oh, this is a
recovering Perl hacker. I picked it up from another programmer I work
with, and looking over what he had initially, I was able to go, oh,
this is a recovering PHP hacker. I want my code to look like, oh, this
is a Ruby hacker. How do I hide my shameful past?
--
Andrew Libby
Tangeis, LLC
Innovative IT Management Solutions
alibby@tangeis.com