How to Get the Name of a Ruby Class

How do I get the name of a Ruby class?

You want to call .name on the object's class:

result.class.name

ruby - get name of class from class method

Remove .class:

class A
def self.get_class_name
self.name.underscore.capitalize.constantize
end
end

self in a context of a class (rather than the context of an instance method) refers to the class itself.

This is why you write def self.get_class_name to define a class method. This means add method get_class_name to self (aka A). It is equivalent to def A.get_class_method.

It is also why when you tried self.class.name you got Class - the Object#class of A is Class.

To make this clearer, consider the output of:

class A
puts "Outside: #{self}"

def self.some_class_method
puts "Inside class method: #{self}"
end

def some_instance_method
puts "Inside instance method: #{self}"
end
end

A.some_class_method
A.new.some_instance_method

Which is:

Outside: A
Inside class method: A
Inside instance method: #<A:0x218c8b0>

How to get the class name from an instance variable in Rails?

Use @commentable.class.name to find out the variable's class name.

Getting the name of the calling class in Ruby

You typically use ruby modules by including them. Try this:

module AwesomeModule
def get_caller
self.class
end
end

class AwesomeClass
include AwesomeModule
def initialize
get_caller
end
end

a = AwesomeClass.new # "AwesomeClass"

Also, note that in your question get_caller is being called on the AwesomeModule module itself, further complicating the issue.

Get a class by name in Ruby?

If you want something simple that handles just your special case you can write

Object.const_get("Admin").const_get("MetaDatasController")

But if you want something more general, split the string on :: and resolve the names one after the other:

def class_from_string(str)
str.split('::').inject(Object) do |mod, class_name|
mod.const_get(class_name)
end
end

the_class = class_from_string("Admin::MetaDatasController")

On the first iteration Object is asked for the constant Admin and returns the Admin module or class, then on the second iteration that module or class is asked for the constant MetaDatasController, and returns that class. Since there are no more components that class is returned from the method (if there had been more components it would have iterated until it found the last).

How do I get the parent's class name in Ruby

class A
end

class B < A
end

B.superclass # => A
B.superclass.name # => "A"

How to get only class name without namespace

The canonical way to do this is to invoke Object#class and Module#name. For example:

bar.class.name.split('::').last
#=> "Bar"

Get class name from a class nested in a module in ruby

You can do:

AisisWriter::Controllers::CommentsManagement::CommentsHandler.name.split('::').last || ''

get class name of the immediate class, not full class name. ruby

In Rails you can do it as: A::B::C.name.demodulize.

Example:

Loading development environment (Rails 4.2.7.1)
[1] pry(main)> module A
[1] pry(main)* class Base;end
[1] pry(main)* module B
[1] pry(main)* class C < A::Base
[1] pry(main)*
[1] pry(main)* def some_method
[1] pry(main)*
[1] pry(main)* end
[1] pry(main)* end
[1] pry(main)* end
[1] pry(main)* end
=> :some_method
[2] pry(main)> A::B::C.name.demodulize
=> "C"
[3] pry(main)>

Look at the documentation of #demodulize

Removes the module part from the constant expression in the string.



Related Topics



Leave a reply



Submit