Tom Jordan
8/15/2006 9:26:00 PM
ruby version:
ruby 1.8.4 (2006-04-14) [i386-mswin32]
On 8/15/06, Tom Jordan <tdjordan@gmail.com> wrote:
> Is there a way to compare two blocks with each other?
>
> If you promote a block into a Proc, how would you know that the
> original block was the same?
>
> This is what I want to do
>
> (1..10).each do
> once { puts "Hi Tom"}
> end
>
> I've tried to implement once like this
>
> class << self
> def once(&block)
> @ids ||= {}
> block.call unless @ids.key? block.object_id
> @ids[block.object_id] = :true
> puts @ids.keys.join(', ')
> end
> end
>
> I've even tried a varient using == and ===
>
> def l(&b)
> @t ||= b
> puts b == @t
> puts b === @t
> @t = b
> end
>
> (1..2).each { l{x=1} }
>
> output:
> true
> true
> false
> false
>
> How can you compare two procs that have the same body?
>
> Thanks,
> -- Tom.
>
> --
> "Nothing will ever be attempted, if all
> possible objections must first be
> overcome." - Samuel Johnson
>
> "Luck is what happens when
> preparation meets opportunity." - Seneca
>
--
"Nothing will ever be attempted, if all
possible objections must first be
overcome." - Samuel Johnson
"Luck is what happens when
preparation meets opportunity." - Seneca