From: dblack@... Date: 2006-04-23T21:15:54+09:00 Subject: Re: Metaprogramming is fun! Hi -- On Sun, 23 Apr 2006, Michael Guterl wrote: > I finally decided to dive in and give metaprogramming in Ruby a shot. I'm > not sure that my example is exactly practical, but it seemed useful at the > time. There's a few areas I would like to hear others suggestions on: > > 1. Does the syntax appear to be in line with the community standards? You're indenting one space instead of two on the first indent, but generally it looks good. > 2. Are my uses of class_eval and instance_eval okay / is there a better way? See below. > require 'rubygems' > require 'facets' > require 'dictionary' > > class FixedLength > > def self.structure(ordered_hash) > > class_eval do > @@structure = ordered_hash > end > > keys = @@structure.keys > instance_eval do > attr_accessor *keys > end I'm not sure why you're using all these *_eval calls. Try this: def self.structure(ordered_hash) @@structure = ordered_hash attr_accessor *@@structure.keys end > end > > # this entire method could probably be a lot cleaner > > def self.open(file_name) > class_eval do I haven't tested it but I don't see any reason for that one either. > data = IO.read(file_name) > records = [] > data.each_line do |line| > last_position = 0 > record = self.new > @@structure.each_pair do |name, length| > record.instance_variable_set( "@#{name.to_s}", The #{} thing does an automatic to_s. Also, since you've gone to the trouble of creating accessors, why not do: record.send("#{name}="), line.slice... > line.slice(last_position, > length.to_i).strip) > last_position += length.to_i > end > records << record > end > return records > end > end > > end You could tighten that method up a bit. Here's an untested rewrite; see if this is of any use: def self.open(file_name) records = [] File.open(file_name) do |fh| fh.each_line do |line| record = new @@structure.each do |name,len| record.send("#{name}=", line.slice!(0,len)) end records << record end end return records end (I'll save my reflections on the likelihood of class variables being necessary for another time :-) David -- David A. Black (dblack@wobblini.net) Ruby Power and Light, LLC (http://www.rubypowerandlight.com) "Ruby for Rails" PDF now on sale! http://www.manning.com/black Paper version coming in early May!