Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[ANN] Article: An Exercise in Metaprogramming with Ruby

17 views
Skip to first unread message

rubyh...@gmail.com

unread,
Mar 24, 2006, 2:37:26 PM3/24/06
to
I failed to post this link before, so here it is now:

http://www.devsource.com/article2/0,1895,1928561,00.asp

My editor said it "didn't do that well" in terms of page views. And I
said,
well, I should have posted it to ruby-talk. And she said: Do that now,
and we'll see what effect it has.

So there you have it. No bots or artificial inflation, please. ;)

This article, by the way, was adapted from a talk given in January
to the Austin on Rails group.


Cheers,
Hal

Matthew Moss

unread,
Mar 24, 2006, 2:52:57 PM3/24/06
to
Interesting article... just about the level I needed. A decent
example, not uselessly trivial, but not terribly complex either, so I
can follow enough of the metaprogramming to truly understand what's
going on.

Thanks...

John W. Long

unread,
Mar 24, 2006, 3:02:48 PM3/24/06
to
Hal wrote:
> I failed to post this link before, so here it is now:
>
> http://www.devsource.com/article2/0,1895,1928561,00.asp
>

Wow. Very nice. I'm doing a fair amount of meta programing myself these
days and having resources like this for reference and information is great.

I like the way you dynamically create the class using Object#const_set.
Metaprogramming sure beats generating code the traditional way.

--
John Long
http://wiseheartdesign.com


pat eyler

unread,
Mar 24, 2006, 3:13:28 PM3/24/06
to
> I failed to post this link before, so here it is now:
>
> http://www.devsource.com/article2/0,1895,1928561,00.asp
>
> My editor said it "didn't do that well" in terms of page views. And I
> said,
> well, I should have posted it to ruby-talk. And she said: Do that now,
> and we'll see what effect it has.

Nice article Hal. It's a shame ZD requires registration to comment
there though.

>
> So there you have it. No bots or artificial inflation, please. ;)


I'll be sharing it around at work, just a bit of natural inflation :)

>
> This article, by the way, was adapted from a talk given in January
> to the Austin on Rails group.
>
>
> Cheers,
> Hal
>
>
>


--
thanks,
-pate
-------------------------


James Edward Gray II

unread,
Mar 24, 2006, 3:28:21 PM3/24/06
to
On Mar 24, 2006, at 1:38 PM, rubyh...@gmail.com wrote:

> I failed to post this link before, so here it is now:
>
> http://www.devsource.com/article2/0,1895,1928561,00.asp

That's so darn cool, I think I'm just going to have to add it to
FasterCSV... ;)

Excellent article Hal!

James Edward Gray II


Bill Guindon

unread,
Mar 24, 2006, 3:48:58 PM3/24/06
to
On 3/24/06, Matthew Moss <matthew.m...@gmail.com> wrote:
> Interesting article... just about the level I needed. A decent
> example, not uselessly trivial, but not terribly complex either, so I
> can follow enough of the metaprogramming to truly understand what's
> going on.

+1

Great stuff Hal, thanks much.

> Thanks...
>
>
> On 3/24/06, rubyh...@gmail.com <rubyh...@gmail.com> wrote:
> > I failed to post this link before, so here it is now:
> >
> > http://www.devsource.com/article2/0,1895,1928561,00.asp
> >
> > My editor said it "didn't do that well" in terms of page views. And I
> > said,
> > well, I should have posted it to ruby-talk. And she said: Do that now,
> > and we'll see what effect it has.
> >
> > So there you have it. No bots or artificial inflation, please. ;)
> >
> > This article, by the way, was adapted from a talk given in January
> > to the Austin on Rails group.
> >
> >
> > Cheers,
> > Hal
> >
> >
> >
>
>


--
Bill Guindon (aka aGorilla)
The best answer to most questions is "it depends".


Ernest Obusek

unread,
Mar 24, 2006, 5:07:29 PM3/24/06
to
I'm a Ruby newbie. So far I am loving everything I learn about
Ruby. I'm trying to find a real app to create with it. I have need
for a client program that talks to an LDAP server and that makes
calls to an ONC/RPC server that we wrote here at my job in C++. Do
these exist for Ruby?

Thanks!

Ernest

Keith Sader

unread,
Mar 24, 2006, 9:30:20 PM3/24/06
to
Question along these lines, suppose you add an attribute to the
'People' class after the initial creation (say by adding another
column to the people.txt file), do the 'old' people classes get the
new attribute as well? If so, what's the initial value? I suspect it
would be nil.

thanks,


--
Keith Sader
ksa...@gmail.com
http://www.saderfamily.org/roller/page/ksader


Andrew Johnson

unread,
Mar 24, 2006, 9:55:26 PM3/24/06
to
Of course, un-pedagogically, it could be compressed a tad:

class DataRecord

def self.make(file_name)
header = File.open(file_name){|f|f.gets}.split(/,/)
struct = File.basename(file_name,'.txt').capitalize
record = Struct.new(struct, *header)

class<<record;self;end.send :define_method, :read do
File.open(file_name) do |f|
f.gets
f.inject([]){|a,l| a << record.new(*eval("[#{l}]")) }
end
end

record
end

end

data = DataRecord.make('people.txt')
list = data.read # or: Struct::People.read
person = list[2]
puts person.name
if person.age < 18
puts "under 18"
else
puts "over 18"
end
puts "Weight is: %.2f kg" % kg = person.weight / 2.2

cheers,
andrew

--
Andrew L. Johnson http://www.siaris.net/
What have you done to the cat? It looks half-dead.
-- Schroedinger's wife

Benjohn Barnes

unread,
Mar 25, 2006, 6:38:39 AM3/25/06
to

On 24 Mar 2006, at 19:38, rubyh...@gmail.com wrote:

> I failed to post this link before, so here it is now:
>
> http://www.devsource.com/article2/0,1895,1928561,00.asp

Thanks very much, you've inspired me to clean up some crusty code
I've been ignoring for a while!


Meinrad Recheis

unread,
Mar 25, 2006, 6:43:21 AM3/25/06
to
> I failed to post this link before, so here it is now:
>
> http://www.devsource.com/article2/0,1895,1928561,00.asp
>
[...]

i like the article!

yeah, and even if code using the fields is coupled tightly to the
created classes, the solution is highly reusable.
-- henon


Joel VanderWerf

unread,
Mar 25, 2006, 7:00:42 PM3/25/06
to

The point about coupling (mentioned in the second-to-last paragraph of
the article) is important, and I feel it is dismissed to easily in the
article. There are some tradeoffs to consider, though perhaps they are
out of the scope of the article, which is intended as an exercise, not
as a complete guide:

1. Suppose your code needs to _discover_ what fields are in the file?
You can use #instance_methods(false), but that is not perfect: you have
to filter out "to_s" and "inspect", which were added by #make. And what
if you add a new method in addition to the ones generated by #make? The
field names could be stored in a list kept in class instance variable...

2. Once you have discovered the field names, you have to use
send(fieldname) and send("#{fieldname}=") to access them. That's more
awkward and (at least in the second case) less efficient than Hash#[]
and #[]=. Who's the "second class citizen" in this case?

3. If you really know the field names "in advance" (that is, you have
enough information to hard code them into your program), rather than "by
discovery", then maybe it is better to use a different metaprogramming
style in which the fields are declared using class methods:

class Person
field :name, :favorite_ice_cream, ...
end

In this way, some rudimentary error checking can be performed when
reading the file, rather than failing later when trying to serve ice
cream to the person. (I just hate it when my ruby-scripted robo-fridge
serves me passion fruit and rabbit dropping ice cream.) This is not
always the best way to go (what if, as the article points out, fields
get added to the file?), but one more thing to keep in mind.

4. Is it always a good idea to couple the class name with the file name?
Maybe the class's identity should be associated with the set of fields
defined in the header? Why not _reuse_ the anonymous class if the header
is the same as those in some other file you imported earlier? (This
could be done using a hash mapping sorted lists of column names to
classes.) That would make it possible to use == to compare objects read
from different files. Further, it would let you use x.class == y.class
to determine if x and y came from files with compatible formats (same
fields, but maybe in a different order).

5. Maybe Struct would serve just as well, since it takes care of
everything in the class_eval block. For example:

klass = Struct.new(*names.map{|s|s.intern})

None of these are necessarily problems, depending on what you are trying
to do, but alternate solutions (for example, using hashes) are worth
considering. Metaprogramming is not always the best solution, though it
is good to have it in your pocket.

Some minor quibbles:

1. In DataRecord.make, if the file happens to be empty, data.gets.chomp
will raise an exception and the file will not be closed. Similarly in
the #read method of the generated class. Why not use a block with File.open?

2. The second way of referring to the class:

require 'my-csv'
DataRecord.make("people.txt") # Ignore the return value and
list = People.read # refer to the class by name.

should raise the hackles on a ruby programmer's neck. It's a violation
of DRY: you have typed the string "people" in two places, and your
program breaks if (a) the filename changes or (b) the way "people.txt"
is transformed into "People" changes. Maybe you _want_ that breakage
(maybe you want the program to fail if someone tries to run it on
"other-people.txt" or on "places.txt"). Or maybe not: it's another
tradeoff. (To be fair, the article doesn't claim that the version with
People hard-coded can read places.txt.)

3. Is it really a good idea to encourage people to eval("[#{line}]") ???

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407


ara.t....@noaa.gov

unread,
Mar 26, 2006, 1:16:15 AM3/26/06
to

i'm totally with you on this joel. still, i think one can have a bit of both:


harp:~ > cat a.rb
require "arrayfields"
require "csv"

csv = <<-csv
latitude,longitude,description
47.23,59.34,Omaha
32.17,39.24,New York City
73.11,48.91,Carlsbad Caverns
csv


class CSVTable < ::Array
attr "fields"
def initialize arg
CSV::parse(arg) do |row|
row.map!{|c| c.to_s}
if @fields
self << row
else
@row_class = Class::new(::Array) do
define_method("initialize") do |a|
self.fields = row
replace a
end
end
@fields = row
end
end
@fields.each{|field| column_attr field}
end
def << row
super @row_class::new(row)
end
def column_attr(ca)
singleton_class = class << self; self; end
singleton_class.module_eval{ define_method(ca){ map{|r| r[ca]}} }
end
def [](*a, &b)
m = a.first
return(send(m)) if [String, Symbol].map{|c| c === m}.any? && respond_to?(m)
super
end
end

table = CSVTable::new csv

p table
puts

p table.fields
puts

table.fields.each{|f| puts "#{ f }: #{ table[f].join(', ') }"}
puts

table.each{|row| puts row.fields.map{|f| "#{ f }: #{ row[f] }"}.join(', ') }
puts


harp:~ > ruby a.rb
[["47.23", "59.34", "Omaha"], ["32.17", "39.24", "New York City"], ["73.11", "48.91", "Carlsbad Caverns"]]

["latitude", "longitude", "description"]

latitude: 47.23, 32.17, 73.11
longitude: 59.34, 39.24, "48.91
description: Omaha, New York City, Carlsbad Caverns

latitude: 47.23, longitude: 59.34, description: Omaha
latitude: 32.17, longitude: 39.24, description: New York City
latitude: 73.11, longitude: 48.91, description: Carlsbad Caverns

regards.


-a
--
share your knowledge. it's a way to achieve immortality.
- h.h. the 14th dali lama


Joel VanderWerf

unread,
Mar 26, 2006, 3:14:26 PM3/26/06
to
..

> class CSVTable < ::Array
> attr "fields"

Sure, that's more or less what I meant by storing the fields, but I was
thinking of using a class instance variable to keep that information at
the class level (assuming you might want to reuse one table class and
one row class for several files).

Using arrayfields is nice, since you have the symbolic #[] and #[]=
interfaces, as with hashes, as well as the array interface. But you have
neither a declared list of what fields should be in the file (what I was
suggesting for error checking purposes), nor the ability to refer to
fields directly with a "first class citizen" method (what Hal's article
was advocating):

p table[1].latitude

Nothing wrong with any of these approaches, it's just good to be aware
of all of them.

Btw, you can use a block with #any? :

> return(send(m)) if [String, Symbol].map{|c| c === m}.any? &&
> respond_to?(m)

return(send(m)) if [String, Symbol].any? {|c| c === m} && respond_to?(m)

Hal Fulton

unread,
Mar 26, 2006, 6:04:43 PM3/26/06
to
Joel VanderWerf wrote:

[snip snip snip]

Joel,

Thanks for your comments. I have read them with interest
(and everyone else's) but am too busy to reply at length.

In short, yes, there are flaws in the approach I took, and
there is more than one way to do it. For the most part, it's
just an exercise.


Thanks,
Hal

James Edward Gray II

unread,
Mar 28, 2006, 7:05:06 PM3/28/06
to
On Mar 24, 2006, at 2:28 PM, James Edward Gray II wrote:

> On Mar 24, 2006, at 1:38 PM, rubyh...@gmail.com wrote:
>
>> I failed to post this link before, so here it is now:
>>
>> http://www.devsource.com/article2/0,1895,1928561,00.asp
>
> That's so darn cool, I think I'm just going to have to add it to
> FasterCSV... ;)

Developer at play:

$ irb -r lib/faster_csv.rb
>> class FullName < Struct.new(:first, :last)
>> def initialize( first, last, other = Hash.new )
>> super(first, last)
>> @middle, @suffix = other.values_at(:middle, :suffix)
>> end
>> attr_accessor :middle, :suffix
>> end
=> nil
>> names = [ FullName.new("Santa", "Clause"),
?> FullName.new("James", "Gray", :middle =>
"Edward", :suffix => "II"),
?> FullName.new("Easter", "Bunny") ]
=> [#<struct FullName first="Santa", last="Clause">, #<struct
FullName first="James", last="Gray">, #<struct FullName
first="Easter", last="Bunny">]
>> csv = FasterCSV.dump(names)
=> "class,FullName\n@middle,@suffix,first=,last=\n,,Santa,Clause
\nEdward,II,James,Gray\n,,Easter,Bunny\n"
>> puts csv
class,FullName
@middle,@suffix,first=,last=
,,Santa,Clause
Edward,II,James,Gray
,,Easter,Bunny
=> nil
>> reloaded = FasterCSV.load(csv)
=> [#<struct FullName first="Santa", last="Clause">, #<struct
FullName first="James", last="Gray">, #<struct FullName
first="Easter", last="Bunny">]
>> reloaded.find { |name| name.first == "James" }.middle
=> "Edward"

That's using the development version of FasterCSV. Thanks for the
idea Hal! ;)

James Edward Gray II

0 new messages