Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ruby

Let assume I have a classes A and B where B inherits A. How do I print parent class name in B

class A end  class B < A end 

Some things I have tried

>> B.new.class #=> B   #which is correct >> B.new.parent  #=> Undefined method `parent` >> B.parent   #=> Object >> B.parent.class #=> Class 

Thanks :)

like image 599
Rahul Tapali Avatar asked Feb 08 '13 18:02

Rahul Tapali


People also ask

What is the parent class Ruby?

But from Ruby 1.9 version, BasicObject class is the super class(Parent class) of all other classes in Ruby. Object class is a child class of BasicObject class.

What is the parent class of parent class?

Parent class is the class being inherited from, also called base class. Child class is the class that inherits from another class, also called derived class.

Does Ruby have inheritance?

In Ruby, single class inheritance is supported, which means that one class can inherit from the other class, but it can't inherit from two super classes. In order to achieve multiple inheritance, Ruby provides something called mixins that one can make use of.

What does .class mean in Ruby?

What is a class in Ruby? Classes are the basic building blocks in Object-Oriented Programming (OOP) & they help you define a blueprint for creating objects. Objects are the products of the class.


1 Answers

class A end  class B < A end  B.superclass # => A B.superclass.name # => "A" 
like image 189
Sergio Tulentsev Avatar answered Oct 03 '22 09:10

Sergio Tulentsev