Michael Judge wrote:
> I'm working on a survey system in Ruby and am having a style crisis with 
> a simple bit of code that has left me paralyzed and unable to work.

Here is the rest of my solution.

Does this help?


Hal


class Respondent

   Fields = { :this => 0..2,
              :that => 3..6,
              :etc  => 7..10 }

   Fields.each_pair do |field,range|
     # add a reader basically doing @resp[range]
     define_method(field) { @resp[range] }
     # add a writer basically doing @resp[range]=val
     define_method((field.to_s+"=").to_sym) do |val|
       raise "Wrong field length" if val.size != range.to_a.size
       @resp[range] = val
     end
   end

   def initialize(str)
     @resp = str
   end

end


resp = Respondent.new("abcdefghij")

puts resp.this
puts resp.that
puts resp.etc

resp.that = "foob"

puts resp.this
puts resp.that
puts resp.etc

resp.that = "foo"  # error!