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"
Rails: Get the Class Name of a Child Class in a Parent Class Method
I don't think you can avoid having some knowledge of the specific subclass if you want the type set correctly, however you could streamline the code so there is far less code duplication. Something like:
class Vote
def self.cast_vote_of_type(params, subtype)
....first_or_create(value: value, type: subtype)
end
end
class Tag::Vote
def self.cast_vote(params)
cast_vote_of_type(params, self.class.name)
end
end
How to get Child class in inherited parent method in Ruby?
In a class method like your find_by_id
:
class ModelBase
def self.find_by_id(id)
p self.class.name
end
end
self
is the class itself (so self.class
is Class
) so you want to look at self.name
:
class ModelBase
def self.find_by_id(id)
p name
end
end
Look up all descendants of a class in Ruby
Here is an example:
class Parent
def self.descendants
ObjectSpace.each_object(Class).select { |klass| klass < self }
end
end
class Child < Parent
end
class GrandChild < Child
end
puts Parent.descendants
puts Child.descendants
puts Parent.descendants gives you:
GrandChild
Child
puts Child.descendants gives you:
GrandChild
Class Method to 'Know' Class Name in Ruby?
A Class Method is a method where the CLASS is the receiver, so to find the object upon which the method is invoked (what you appear to be trying to do here) simply inspect the value of self
.
class Parent
def self.whoami
self
end
end
class Child < Parent
end
puts Parent.whoami #=> Parent
puts Child.whoami #=> Child
Get Ruby pure class name without parent name, any direct method for it?
Classes don't really have "names" in Ruby. A class is an object like any other object, it gets assigned to variables like any other object. A class doesn't have a name, just like a number or a string doesn't have a name.
Take this example:
foo = Class.new
Bar = foo
Baz = foo
Bar = nil
remove_const :Bar
What should the "name" of the class be in this example?
Well, there is a method called name
in the Module
class. What it does is the following: if the class object has been assigned to a constant for the first time, the name of that constant (note that even that is a fuzzy concept!) becomes the name of the class, otherwise the name will just be nil
.
Expanding on the example above:
foo = Class.new
foo.name
# => nil
Bar = foo
foo.name
# => 'Bar'
Bar.name
# => 'Bar'
Baz = foo
Baz.name
# => 'Bar'
Bar = nil
remove_const :Bar
foo.name
# => 'Bar'
Here's another example:
foo = Class.new
Bar = foo
Baz = foo
class Baz
class Quux; end
Xyzzy = Quux
end
foo::Xyzzy.name
# => 'Bar::Quux'
Note that even though Quux
is defined inside Baz
and accessed via foo::Xyzzy
it still prints Bar::Quux
as its name.
Also, two different classes can have the same name:
Foo = Class.new
Bar = Foo
Foo = nil
remove_const :Foo
Foo = Class.new
Baz = Foo
Foo = nil
remove_const :Foo
Bar.name
# => Foo
Baz.name
# => Foo
Bar == Baz
# => false
The "name" of a class is simply a debugging help for human readers, you should never use it for anything else, and never use it programmatically (or even depend on a specific structure of the string).
How can I determine the child class name from abstract parent class in Ruby
You can try using the inherited
hook:
class BaseClass
def self.inherited(child_class)
child_class.class_eval do
belongs_to :parent, :class_name => child_class.name
end
super
end
end
Related Topics
How to Call a Super Class Method
Combinatory Method Like Tap, But Able to Return a Different Value
Indentation Sensitive Parser Using Parslet in Ruby
How to Get Long Url from Short Url
Is It Right to Assign Multiple Variables Like This a = B = C = D = 5
Mountain Lion - Libxml & Nokogiri
How to Save Settings as a Hash in a External File
Thread.Join Blocks the Main Thread
Ruby Check If Nil Before Calling Method
Implementation of "Remember Me" in a Rails Application
How to Get the Ip Address of My Local MAChine in Ruby
Openssl VS Gpg for Encrypting Off-Site Backups
Solutions to the Annoying "Warning: Already Initialized Constant" Message