"alias name objetc.method" not allowed?

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?

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

2008/4/25, David A. Black [email protected]:

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

On Fri, Apr 25, 2008 at 1:52 PM, David A. Black [email protected]
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

2008/4/25, David A. Black [email protected]:

object.hello
method definitions are for :slight_smile:
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 !!!

Hi –

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

2008/4/25, David A. Black [email protected]:

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 :slight_smile:

David

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs