From: Dark Ambient Date: 2006-07-12T03:18:55+09:00 Subject: Re: blocks and procs ------=_Part_169784_31275581.1152641929103 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline oops...this is the error: rb:32:in `select': wrong number of arguments (0 for 1) (ArgumentError) On 7/11/06, Dark Ambient wrote: > > Good example. Also because it's rails centric (something I'd eventually > like to get to) > I am running into some select errors though , via the select statement. > If your up to reading it through (I don't find any typos matched against > your code), if not that is fine as the explanation helped me. > > class ClientDB > Record = Struct.new(:client_name, :location, :projects) > > def initialize > @records = [ Record.new("Gray Productions", "Oklahoma", > ["Ruby Quiz", "Rails Extensions"] ), > Record.new( "Serenity Crew", "Deep Space", > ["Ship Enhancements"] ), > Record.new( "Neo", "Hollywood", > ["Rails interface for the Matrix"] ) ] > end # end of method > > def select( query ) > # parse query String > rules = Hash[*query.split(/\s*AND\s*/).map do |rule| > rule.split(/\s*=\s*/).map { |value| value.sub(/^['"](.+)['"]$/, > '\1') } > end.flatten ] > > #match records > @records.select { |record| block_given? and yield record } > rules.all? { |field, value| record.send(field) == value } > > > end # end of method > > end # end of class > > require "pp" > db = ClientDB.new > pp db.select { |record| record.client_name != "Gray Productions" } > pp db.select { |record| record.client_name +~ /crew/i } > pp db.select { |record| record.projects.size == 1 } > pp db.select { |record| record.projects.include? "Ruby Quiz" } > > -------- > > > On 7/11/06, James Edward Gray II < james@grayproductions.net> wrote: > > > > On Jul 11, 2006, at 11:55 AM, Dark Ambient wrote: > > > > > Just curious, I'm just learning about Procs, and have already > > > looked at > > > using blocks. > > > Are they very close in the way they function. It seems that way to > > > me. > > > Perhaps I'm wrong. > > > Is one favoured above the other ? > > > > I see David has already answered your question here, but just in case > > it helps I wrote about this on my blog a while back: > > > > http://blog.grayproductions.net/articles/2006/01/05/code-as-a-data-type > > > > James Edward Gray II > > > > > ------=_Part_169784_31275581.1152641929103--