[LRUG] Looping through classes

Ronny Ager-Wick ronny at ager-wick.com
Wed Mar 13 17:38:26 PDT 2013


I've been trying to find a way to do this, but I may be searching for the 
wrong terms or something because I get nowhere, even with the help of my 
friend Google.
I want to loop through the defined classes to see which ons of them responds 
to certain methods.
The classes I'm trying to find are Ohm Models that have a certain attribute 
defined.
If I had a collection of classes, I'd probably do something like this:

classes = #get list of all classes
classes.each { |c|
   # assume it's an Ohm::Model if it responds to :first
   # maybe there's a better way of filtering out only the Ohm models
   if c.respond_to?(:first) && c.first.respond_to?(:updated_at)
     # convert the timestamp of all records in this model.
   end
end

The end objective is to convert the timestamp from the old way they used to 
store it (Ohm-contrib 0.*) which looks like this "2011-02-25 21:54:06 UTC", 
and the new way, which are UNIX timestamps.
I could always just make a list of the models manually and loop through them, 
but I want to make a reusable script that others can benefit from as well.
Ronny.



More information about the Chat mailing list