There were two use cases given in this thread. Whether there was a way
to do the same thing otherwise doesn not neccessarily deligitimize the
potential use.
To be more specific in my case I have a method:
class AClass
initialize( ¶ms )
params.call(self)
end
end
Configuration of the class needs to be settable either via a block:
AClass.new { |s|
s.conf = "whatever"
}
But also from an external hash-like object.
hl = HashLike.new( { :conf => 'whatever' } )
AClass.new( &hl )
But this later won't work b/c of the Proc restriction. And, just as I
started this thread, I see no good reason that it should restrict.
Someone mentioned speed at one point, but that's not Ruby's modus
operandi --functionality is. A simple check for a "to_p" is reasonable
if the object isn't a strict Proc.
Now you say there is no usecase. Lets look at my alternatives here. One
is to change the interface to accept both a hash-like object and a proc
--oh the joy:
initialize( paramsh={}, ¶ms )
raise "bugger" if paramsh and params
if params
params.call(self)
else
# this would have been the HashLike#call method
paramsh.each { |k,v| self.send("#{k}=",v) }
end
end
Okay, so something like that can work but it isn't very OOP or DRY and
more importantly my use case is for something I want end-programmers to
be able to easily use to create there own "task interfaces" --this mess
just doesn't cut it.
So here's what I had to do to make it work for my usecase. Keeping the
original simple definition I added a method to the HashLike class such
that:
hl = HashLike.new( { :conf => 'whatever' } )
AClass.new( &(hl.to_proc) )
Ain't it ironic. I had to take a hash, convert it to a proc, just so I
can turn around and convert it back to a set of attribute parameters.
Okay. So you say there's no usecase. You're younger than I, Eric, so
your mind is surely more supple. Maybe you are right. Maybe there's a
better way to do this. Have any idea?
T.
P.S. Barring proof that there is no real usecase for it, I'll make the
Hash#to_proc method available in the upcoming version of Facets.