Is posible to get the Module(s), Class and Method names in which we are?

Hi, for logging purposes I'd like to print the Modules, Class and Method in
with the logger is called. Imagine this case:

module App
  module TransportLayer
    class Server
      def get_data()
        ...
        ...
        logger.info "We are in XXXXXXX"
      end
    end
  end
end

I'd like to see:
  "We are in App::TransportLayer::Server#get_data"

Is it possible in anyway? The only I know how to do if getting the current
Class name by doing "self.class.to_s".

Thanks a lot.

···

--
Iñaki Baz Castillo

Ok sorry, I can get the modules and classes via "self.class.to_s":
=> "App::Transport::Server::TCP"

The only I miss if the method name, is ti possible?

Thanks.

···

El Domingo, 6 de Abril de 2008, Iñaki Baz Castillo escribió:

module App
  module TransportLayer
    class Server
      def get_data()
        ...
        ...
        logger.info "We are in XXXXXXX"
      end
    end
  end
end

I'd like to see:
  "We are in App::TransportLayer::Server#get_data"

Is it possible in anyway? The only I know how to do if getting the current
Class name by doing "self.class.to_s".

--
Iñaki Baz Castillo

Iñaki Baz Castillo wrote:

The only I miss if the method name, is ti possible?

In 1.9 there is Method#name, but in 1.8.6 I think your best bet is to parse the output from Kernel#caller.

···

--
RMagick: http://rmagick.rubyforge.org/
RMagick 2: http://rmagick.rubyforge.org/rmagick2.html

Yes, from caller().

Kind regards

  robert

···

On 06.04.2008 14:39, Iñaki Baz Castillo wrote:

El Domingo, 6 de Abril de 2008, Iñaki Baz Castillo escribió:

module App
  module TransportLayer
    class Server
      def get_data()
        ...
        logger.info "We are in XXXXXXX"
      end
    end
  end
end

I'd like to see:
  "We are in App::TransportLayer::Server#get_data"

Is it possible in anyway? The only I know how to do if getting the current
Class name by doing "self.class.to_s".

Ok sorry, I can get the modules and classes via "self.class.to_s":
=> "App::Transport::Server::TCP"

The only I miss if the method name, is ti possible?

Thanks, I'll try it.

···

El Domingo, 6 de Abril de 2008, Tim Hunter escribió:

Iñaki Baz Castillo wrote:
> The only I miss if the method name, is ti possible?

In 1.9 there is Method#name, but in 1.8.6 I think your best bet is to
parse the output from Kernel#caller.

--
Iñaki Baz Castillo

Look at Facets: facets/ruby.rb. There is 1.8 implementation of
#__method__.

    # Retreive the current running method name.

···

On Apr 6, 9:25 am, Iñaki Baz Castillo <i...@aliax.net> wrote:

El Domingo, 6 de Abril de 2008, Tim Hunter escribió:

> Iñaki Baz Castillo wrote:
> > The only I miss if the method name, is ti possible?

> In 1.9 there is Method#name, but in 1.8.6 I think your best bet is to
> parse the output from Kernel#caller.

Thanks, I'll try it.

    #
    # def tester; __method__; end
    # tester #=> :tester
    #
    # Technically __callee__ should provided alias names,
    # where __method__ should not. But we'll have to
    # leave that distinction to Ruby 1.9+.

    def __method__
      /\`([^\']+)\'/.match(caller(1).first)[1].to_sym
    end

T.

Thanks :wink:

···

El Domingo, 6 de Abril de 2008, Trans escribió:

On Apr 6, 9:25 am, Iñaki Baz Castillo <i...@aliax.net> wrote:
> El Domingo, 6 de Abril de 2008, Tim Hunter escribió:
> > Iñaki Baz Castillo wrote:
> > > The only I miss if the method name, is ti possible?
> >
> > In 1.9 there is Method#name, but in 1.8.6 I think your best bet is to
> > parse the output from Kernel#caller.
>
> Thanks, I'll try it.

Look at Facets: facets/ruby.rb. There is 1.8 implementation of
#__method__.

    # Retreive the current running method name.
    #
    # def tester; __method__; end
    # tester #=> :tester
    #
    # Technically __callee__ should provided alias names,
    # where __method__ should not. But we'll have to
    # leave that distinction to Ruby 1.9+.

    def __method__
      /\`([^\']+)\'/.match(caller(1).first)[1].to_sym
    end

--
Iñaki Baz Castillo