[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Delegate to allow subclassing Integer, Float

Clifford Heath

2/27/2008 4:23:00 AM

I have the following code using facets/basicobject with 1.8,
and want to know what the equivalent would be for 1.9:

class Real < BasicObject
def initialize(r)
@__value = r.to_f
end

define_method(:class) { __self__.class }

def send(meth, *a, &b)
# Ensure that normal sends go to __self__, not @__value
__self__.send(meth, *a, &b)
end

private
def method_missing(meth, *args, &block)
@__value.send meth, *args, &block
end
end

(and similar, defining Int for integers).

This seems to be ok with 1.8, but 1.9 doesn't have __self__.

Any thoughts?

Clifford Heath.
4 Answers

Trans

2/27/2008 12:07:00 PM

0

On Feb 26, 11:24 pm, Clifford Heath <n...@spam.please.net> wrote:
> I have the following code using facets/basicobject with 1.8,
> and want to know what the equivalent would be for 1.9:
>
> class Real < BasicObject
> def initialize(r)
> @__value = r.to_f
> end
>
> define_method(:class) { __self__.class }
>
> def send(meth, *a, &b)
> # Ensure that normal sends go to __self__, not @__value
> __self__.send(meth, *a, &b)
> end
>
> private
> def method_missing(meth, *args, &block)
> @__value.send meth, *args, &block
> end
> end
>
> (and similar, defining Int for integers).
>
> This seems to be ok with 1.8, but 1.9 doesn't have __self__.
>
> Any thoughts?

I'm surprised to see __send__ and class are not defined, since it is
easy enough to override them if need be. Creating them from scratch
is a bit tricky --in fact for #class how is it even possible?

Well, not perfect, but you can do:

def class; Real; end

as long as Real won't be subclassed; and

require 'facets/kernel/as'

def send(meth, *a, &b)
send_as(BasicObject, meth, *a, &b) # or
as(BasicObject).send(meth, *a, &b)
end

But why do you not want send to route to method_missing, after all
what other methods are there to call?

T.

Rick DeNatale

2/27/2008 12:47:00 PM

0

On 2/27/08, Trans <transfire@gmail.com> wrote:
> On Feb 26, 11:24 pm, Clifford Heath <n...@spam.please.net> wrote:

> > def send(meth, *a, &b)
> > # Ensure that normal sends go to __self__, not @__value
> > __self__.send(meth, *a, &b)
> > end
...

> > This seems to be ok with 1.8, but 1.9 doesn't have __self__.

> I'm surprised to see __send__ and class are not defined, since it is
> easy enough to override them if need be.

Ermm, he said __self__ not __send__. I don't think I'd ever heard of
__self__ before, well maybe in Python, but not in Ruby.

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

Arlen Cuss

2/27/2008 12:51:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

Hi,

On Wed, Feb 27, 2008 at 11:47 PM, Rick DeNatale <rick.denatale@gmail.com>
wrote:

> > > This seems to be ok with 1.8, but 1.9 doesn't have __self__.
>
> > I'm surprised to see __send__ and class are not defined, since it is
> > easy enough to override them if need be.
>
> Ermm, he said __self__ not __send__. I don't think I'd ever heard of
> __self__ before, well maybe in Python, but not in Ruby.


It's Facets.

Arlen

Clifford Heath

2/27/2008 10:43:00 PM

0

Trans wrote:
> On Feb 26, 11:24 pm, Clifford Heath <n...@spam.please.net> wrote:
>> I have the following code using facets/basicobject with 1.8,
>> and want to know what the equivalent would be for 1.9:
....clip...
>> This seems to be ok with 1.8, but 1.9 doesn't have __self__.

> as long as Real won't be subclassed;

It will be. That's its purpose.

> But why do you not want send to route to method_missing, after all
> what other methods are there to call?

Subclass methods, and eigenclass methods or methods added by define_method.

If you take a look at the example code here:
<http://activefacts.rubyforge.org/svn/examples/ruby/CompanyDirec...

The "binary" class method creates methods in the current class and the
class that plays the role at the other end of this binary relationship.
In this example, there are String subclasses and normal classes, no
Real or Int subclasses, but I want the method to work the same.

The point is that these methods are created on the delegate class, and
will be called by send as well as directly. BasicObject omits send, so
calls to send go to method_missing and get delegated, now what I need.
In the Facets implementation, I can re-implement send using __self__,
but there seems no way to do that in 1.9.

I'm hoping I'm missing something and there is a way to do it.

> require 'facets/kernel/as'

I'm not familiar with "as", I'll read it now.

Clifford Heath.