Jason Kim
Jason Kim

Reputation: 19061

Getting class name and method name in Ruby

Say I have,

class Foo
  def self.bar
    puts "I want to print #{some_method_for_class_name} and #{some_method_for_method_name}."
  end
end

Is there some way to get "Foo" and "bar" in the place for some_method_for_class_name and some_method_for_method_name respectively?

Upvotes: 1

Views: 90

Answers (1)

Matt Glover
Matt Glover

Reputation: 1347

Something like this should work:

class Foo
  def self.bar
    puts "I want to print #{self} and #{__method__}."
  end
end

If it was not a class method you'd need to use self.class instead.

Upvotes: 2

Related Questions