From: Aldric Giacomoni Date: 2010-04-16T21:42:35+09:00 Subject: Re: Writing a parser Martin Hansen wrote: > Hello there, > > I am struggling to get a grip on OO (and Ruby) and write a parser for > this particular data format described at the below get_record method. > The goal is to parse and emit records of this type and be able to > manipulate these with the methods in class Hash. > > For now I am trying to get parsing to work, and the problem here is, > that the add methods is not defined for Hash. What is the best way of > doing that? > > record = {} > block.each_line do |line| > fields = line.split(": ") > raise ArgumentError, "Bad record line: #{line}" if fields.length > != 2 > > record.add(fields[0],fields[1]) > end irb(main):001:0> record = {} => {} irb(main):002:0> record[:name] = "Doe, John" => "Doe, John" irb(main):003:0> record[:eye_color] = "Blue" => "Blue" irb(main):004:0> record[:age] = 5 => 5 irb(main):005:0> record => {:age=>5, :name=>"Doe, John", :eye_color=>"Blue"} -- Posted via http://www.ruby-forum.com/.