# Bind to an "as if" superclass context

**URL:** <https://www.ruby-forum.com/t/bind-to-an-as-if-superclass-context/112989>\
**Category:** Ruby\
**Created:** [August 25, 2007, 3:17pm UTC](https://www.ruby-forum.com/t/bind-to-an-as-if-superclass-context/112989 "2007-08-25T15:17:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [August 25, 2007, 3:17pm UTC](https://www.ruby-forum.com/t/bind-to-an-as-if-superclass-context/112989/1 "2007-08-25T15:17:27Z")

</div>

Is there a way to do the “opposite” of binding a superclass method to  
the current context? In other words, I want to call a method as if the  
current binding were an instance of the superclass. For example, using  
the method #public\_methods as a basis of demonstration (could be any  
method though):

class X  
def a; “a”; end  
end

class Y \< X  
def b; “b”; end

```
def public_methods(all=true)
  # ?

```

self.class.superclass.instance\_method(:public\_methods).bind(self).call(all)  
end  
end

y = Y.new

p y.public\_methods(false)

Of course, that produces

[‘b’, ‘public\_methods’]

But what I want is:

[‘a’]

I suspect there’s no way to do this short of resorting to delegation  
rather than subclassing. But I wanted to ask and make sure.

Thanks,  
T.

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [August 25, 2007, 5:13pm UTC](https://www.ruby-forum.com/t/bind-to-an-as-if-superclass-context/112989/2 "2007-08-25T17:13:33Z")

</div>

On 8/25/07, Trans [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class Y \< X
> 
> I suspect there’s no way to do this short of resorting to delegation  
> rather than subclassing. But I wanted to ask and make sure.

Well, I don’t know about in general, but for this particular instance  
(and similar ones) you could just do  
class X  
def a; “a”; end  
end

class Y \< X  
def b; “b”; end  
def public\_methods(all = true)  
self.class.superclass.public\_instance\_methods(all)  
end  
end

Incidently, you do realize that the whole mess of  
self.class.superclass.instance\_method(:public\_methods).call(all) is  
exactly equivalent to super right? At least in the context of  
overriding public\_methods anyway. And of course def foo(); super; end  
is the same as not writing foo at all, so it’s not surprising you get  
the same result.

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [August 26, 2007, 4:44am UTC](https://www.ruby-forum.com/t/bind-to-an-as-if-superclass-context/112989/3 "2007-08-26T04:44:52Z")

</div>

On Aug 25, 8:12 am, “Logan C.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> self.class.superclass.public_instance_methods(all)
> 
> ```
> 
> end  
> end

Yep. I was hoping to avoid that if possible. It will a chore to makes  
sure I cover all the ones that matter, and that I’ve emulated  
properly.

> Incidently, you do realize that the whole mess of  
> self.class.superclass.instance\_method(:public\_methods).call(all) is  
> exactly equivalent to super right? At least in the context of  
> overriding public\_methods anyway. And of course def foo(); super; end  
> is the same as not writing foo at all, so it’s not surprising you get  
> the same result.

Sure. That was just the “opposite” example of what I was trying to do.  
Ie. rather than binding a super method to the current context, I’m  
trying to bind a method “as if” to a super context.

Also, I realized that delegation is out of the question for what I’m  
trying to do. So I will have to deal with this issue in some fashion.

Thanks,  
T.

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [August 27, 2007, 3:02am UTC](https://www.ruby-forum.com/t/bind-to-an-as-if-superclass-context/112989/4 "2007-08-27T03:02:10Z")

</div>

On 8/25/07, Trans [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Sure. That was just the “opposite” example of what I was trying to do.  
> Ie. rather than binding a super method to the current context, I’m  
> trying to bind a method “as if” to a super context.
> 
> Also, I realized that delegation is out of the question for what I’m  
> trying to do. So I will have to deal with this issue in some fashion.

Had a thought, what if you do “reverse” delegation?  
Instead of having a subclass instance delegate to an instance of super  
class, have a super class delegate to an instance of a subclass

I don’t know how feasible this idea is, as I don’t know your class  
hierarchy, but it’s an idea.

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [August 27, 2007, 5:08pm UTC](https://www.ruby-forum.com/t/bind-to-an-as-if-superclass-context/112989/5 "2007-08-27T17:08:46Z")

</div>

On Aug 26, 6:00 pm, “Logan C.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Had a thought, what if you do “reverse” delegation?  
> Instead of having a subclass instance delegate to an instance of super  
> class, have a super class delegate to an instance of a subclass
> 
> I don’t know how feasible this idea is, as I don’t know your class  
> hierarchy, but it’s an idea.

Thanks. A fair suggestion, that under other circumstances might do the  
trick. Unfortunately, my purposes are quite a bit more difficult. I’m  
attempting to implement Cuts (transparent subclasses) in pure Ruby.  
The essential operation of this is to subclass the original class, but  
have the subclass behave for all “meta-purposes” as if it were the  
original class.

T.
