[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: blocks and procs

James Gray

7/11/2006 5:23:00 PM

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...

James Edward Gray II

2 Answers

James Gray

7/11/2006 6:57:00 PM

0

On Jul 11, 2006, at 1:18 PM, Dark Ambient wrote:

> I am running into some select errors though , via the select
> statement.

Sure I see the trouble...

> 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" }

In order to run this example, you need to use the select() code in
the article, just a paragraph above it. I'm slowly making changes
and refining a solution as I go.

Hope that helps.

James Edward Gray II


James Gray

7/11/2006 7:42:00 PM

0

On Jul 11, 2006, at 2:31 PM, Dark Ambient wrote:

> hmmmm....i have that code in there:
>
>> 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

That's from higher up in the article. Here's the one just before the
example you showed:

class ClientDB
def select
@records.select { |record| block_given? and yield record }
end
end

Try that.

James Edward Gray II