"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?

···

--
Iñaki Baz Castillo
<ibc@aliax.net>

Hi --

···

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

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?

I think you want:

def ob_hello
   object.hello
end

David

--
Rails training from David A. Black and Ruby Power and Light:
   INTRO TO RAILS June 9-12 Berlin
   ADVANCING WITH RAILS June 16-19 Berlin
   INTRO TO RAILS June 24-27 London (Skills Matter)
See http://www.rubypal.com for details and updates!

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

···

2008/4/25, David A. Black <dblack@rubypal.com>:

I think you want:

def ob_hello
  object.hello
end

--
Iñaki Baz Castillo
<ibc@aliax.net>

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

···

On Fri, Apr 25, 2008 at 1:52 PM, David A. Black <dblack@rubypal.com> wrote:

Hi --

I think you want:

def ob_hello
  object.hello
end

Hi --

···

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

2008/4/25, David A. Black <dblack@rubypal.com>:

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

--
Rails training from David A. Black and Ruby Power and Light:
   INTRO TO RAILS June 9-12 Berlin
   ADVANCING WITH RAILS June 16-19 Berlin
   INTRO TO RAILS June 24-27 London (Skills Matter)
See http://www.rubypal.com for details and updates!

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 !!!

···

2008/4/25, David A. Black <dblack@rubypal.com>:

Hi --

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

> 2008/4/25, David A. Black <dblack@rubypal.com>:
>
> > 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:

--
Iñaki Baz Castillo
<ibc@aliax.net>