From: Morton Goldberg Date: 2006-08-14T03:52:10+09:00 Subject: Re: Search objects I'm assuming that you are just implementing this for its educational value and not seriously considering implementing a data base with this approach. Ruby has built-in and third-party libraries to help you implement a data bases application. I've modified your posting just enough to make a shallow scratch on the surface of what can be done to organize and search data. I hope it's enough to help you along. Although you said you didn't want to consider regular expressions yet, I use them because it makes life much simpler to use at least simple regexes. #! /usr/bin/ruby -w class Composer @@composers = [] def Composer.find(attrib, regex) @@composers.select do |c| c.respond_to?(attrib) && c.send(attrib) =~ regex end end def initialize( lname, country ) @lname = lname @country = country @@composers << self end attr_accessor :lname, :country alias :to_s :inspect end Composer.new("Beethoven", "Germany") Composer.new("DeFalla", "Spain") Composer.new("Debussy", "France") Composer.new("Bach", "Germany") Composer.new("Rodrigo", "Spain") Composer.new("Ravel", "France") puts Composer.find(:country, /^Ger/) puts Composer.find(:lname, /^R/) # => # # # # # # # # Regards, Morton On Aug 13, 2006, at 1:54 PM, Dark Ambient wrote: > I can use some help here: > > I've set up this class and objects and would like to "search" it > similar to the way I would search a database. At this point I'm not > concerned with adding regex for more liberal searches. > Any suggestions ? > > > > class Composer > def initialize( lname, country ) > @lname = lname > @country = country > end > attr_accessor(:lname, :country) > end > > comp1 = Composer.new("Beethoven", "Germany") > comp2 = Composer.new("DeFalla", "Spain") > comp3 = Composer.new("Debussy", "France") >