# Delegate to allow subclassing Integer, Float

**URL:** <https://www.ruby-forum.com/t/delegate-to-allow-subclassing-integer-float/130297>\
**Category:** Ruby\
**Created:** [February 27, 2008, 5:25am UTC](https://www.ruby-forum.com/t/delegate-to-allow-subclassing-integer-float/130297 "2008-02-27T05:25:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Clifford\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/49beb7/32.png) [@Clifford\_H](https://www.ruby-forum.com/u/Clifford_H)\
**Post date:** [February 27, 2008, 5:25am UTC](https://www.ruby-forum.com/t/delegate-to-allow-subclassing-integer-float/130297/1 "2008-02-27T05:25:47Z")

</div>

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®  
@\_\_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 H…

---

<div class="post-metadata">

**Author:** ![Clifford\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/49beb7/32.png) [@Clifford\_H](https://www.ruby-forum.com/u/Clifford_H)\
**Post date:** [February 27, 2008, 1:08pm UTC](https://www.ruby-forum.com/t/delegate-to-allow-subclassing-integer-float/130297/2 "2008-02-27T13:08:30Z")

</div>

On Feb 26, 11:24 pm, Clifford H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def send(meth, \*a, &b)  
> (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.

---

<div class="post-metadata">

**Author:** ![Clifford\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/49beb7/32.png) [@Clifford\_H](https://www.ruby-forum.com/u/Clifford_H)\
**Post date:** [February 27, 2008, 1:48pm UTC](https://www.ruby-forum.com/t/delegate-to-allow-subclassing-integer-float/130297/3 "2008-02-27T13:48:15Z")

</div>

On 2/27/08, Trans [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Feb 26, 11:24 pm, Clifford H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![Clifford\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/49beb7/32.png) [@Clifford\_H](https://www.ruby-forum.com/u/Clifford_H)\
**Post date:** [February 27, 2008, 1:51pm UTC](https://www.ruby-forum.com/t/delegate-to-allow-subclassing-integer-float/130297/4 "2008-02-27T13:51:46Z")

</div>

Hi,

On Wed, Feb 27, 2008 at 11:47 PM, Rick DeNatale  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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

---

<div class="post-metadata">

**Author:** ![Clifford\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/49beb7/32.png) [@Clifford\_H](https://www.ruby-forum.com/u/Clifford_H)\
**Post date:** [February 28, 2008, 12:16am UTC](https://www.ruby-forum.com/t/delegate-to-allow-subclassing-integer-float/130297/5 "2008-02-28T00:16:02Z")

</div>

Trans wrote:

> On Feb 26, 11:24 pm, Clifford H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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/CompanyDirector.rb](http://activefacts.rubyforge.org/svn/examples/ruby/CompanyDirector.rb)

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 H…
