--- Christian Neukirchen <chneukirchen / gmail.com> wrote:
> gabriele renzi <surrender_it / remove-yahoo.it> writes:
> > Eric Mahurin ha scritto:
> >
> >> Is there another way to do this?  With all of the talk
> about
> >> duck-typing, there would already be something out there to
> do
> >> this.
> >
> > There was some talk in the past about Object.new taking a
> block, which
> > imho would be useful for this. Meanwhile what about using
> Struct.new
> > with a block to define methods?
> 
> Wow, this is nice... but where is it documented?
> 
> I used to use code like this until now:
> 
> class Foo < Struct.new(:all, :my, :fields)
>   def mymethod
>     # ...
>   end
> end

So is Struct.new(&block) equivalent to:

StructClass = Struct.new
StructClass.instance_eval(&block)
StructClass

So you could do this to make an object using == to map to ===
of a range.

alpha = ('a'..'z')
alphaeq =
Struct.new(:dummy){define_method(:==,&alpha.method(:===))}.new

Actually it looks like you can do the same thing with Class:

alpha = ('a'..'z')
alphaeq = Class.new{define_method(:==,&alpha.method(:===))}.new

This is still pretty verbose, but it seems like a descent
solution.  I'll use this until something better comes along.



		
__________________________________ 
Discover Yahoo! 
Stay in touch with email, IM, photo sharing and more. Check it out! 
http://discover.yahoo.com/stayintouch.html