# "alias name objetc.method" not allowed?

**URL:** <https://www.ruby-forum.com/t/alias-name-objetc-method-not-allowed/135833>\
**Category:** Ruby\
**Created:** [April 25, 2008, 1:39pm UTC](https://www.ruby-forum.com/t/alias-name-objetc-method-not-allowed/135833 "2008-04-25T13:39:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [April 25, 2008, 1:39pm UTC](https://www.ruby-forum.com/t/alias-name-objetc-method-not-allowed/135833/1 "2008-04-25T13:39:42Z")

</div>

Hi, I’ve a method that returns an object:

def object  
return @object  
end

That @object is a object of class MyObject containing methods:

class MyObject  
def hello  
“helloooooooo”  
end  
end

And I’d like to do:

def object  
return @object  
end

alias ob\_hello object.hello

but it’s not allowed, that a pity:  
unexpected ‘.’

So, is not possible to create an alias to a method of a method/object?

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [April 25, 2008, 1:53pm UTC](https://www.ruby-forum.com/t/alias-name-objetc-method-not-allowed/135833/2 "2008-04-25T13:53:16Z")

</div>

Hi –

On Fri, 25 Apr 2008, IÃ±aki Baz C. wrote:

> ```
> "helloooooooo"
> 
> ```
> 
> but it’s not allowed, that a pity:  
> unexpected ‘.’
> 
> So, is not possible to create an alias to a method of a method/object?

I think you want:

def ob\_hello  
object.hello  
end

David

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [April 25, 2008, 2:02pm UTC](https://www.ruby-forum.com/t/alias-name-objetc-method-not-allowed/135833/3 "2008-04-25T14:02:05Z")

</div>

2008/4/25, David A. Black [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I think you want:
> 
> def ob\_hello  
> object.hello  
> end

Yes, sure it works (and it that I’m using now), but since in fact is  
no a new method but just an alias I’d prefer using “alias”  
nomenclature… if it would be possible XD

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [April 25, 2008, 2:06pm UTC](https://www.ruby-forum.com/t/alias-name-objetc-method-not-allowed/135833/4 "2008-04-25T14:06:12Z")

</div>

On Fri, Apr 25, 2008 at 1:52 PM, David A. Black [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hi –
> 
> I think you want:
> 
> def ob\_hello  
> object.hello  
> end

ah yes that might be what OP wants, I will follow another track, though.  
My initial idea was that OP wants to forward messages to the underlying  
object:

require ‘forwardable’  
class Whatever  
extend Forwardable  
def\_delegator :@output, :hello, :obj\_hello  
end

Whatever.new.obj\_hello

This, however does not work on the toplevel.

HTH  
Robert

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [April 25, 2008, 2:36pm UTC](https://www.ruby-forum.com/t/alias-name-objetc-method-not-allowed/135833/5 "2008-04-25T14:36:29Z")

</div>

2008/4/25, David A. Black [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > > object.hello  
> > > method definitions are for 🙂  
> > > Well, I’ve created “pv\_reader”, “pv\_writer” and “pv\_accessor”: XDD

def self.pv\_reader(name, method)  
module\_eval %{  
def #{name}  
#{method}  
end  
}  
end  
def self.pv\_writer(name, method)  
module\_eval %{  
def #{name}= (value)  
#{method}=(value)  
end  
}  
end  
def self.pv\_accessor(name, method)  
self.pv\_reader(name, method)  
self.pv\_writer(name, method)  
end

So I use them as the following:

pv\_accessor :fd :“from.domain”

Ruby is great !!!

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [April 25, 2008, 2:07pm UTC](https://www.ruby-forum.com/t/alias-name-objetc-method-not-allowed/135833/6 "2008-04-25T14:07:23Z")

</div>

Hi –

On Fri, 25 Apr 2008, IÃ±aki Baz C. wrote:

> 2008/4/25, David A. Black [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > I think you want:
> > 
> > def ob\_hello  
> > object.hello  
> > end
> 
> Yes, sure it works (and it that I’m using now), but since in fact is  
> no a new method but just an alias I’d prefer using “alias”  
> nomenclature… if it would be possible XD

alias creates a new name for an existing method. It’s not a  
general-purpose named wrapper around arbitrary code. That’s what  
method definitions are for 🙂

David
