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 :)
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.
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.
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 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.
class A end class B < A end B.superclass # => A B.superclass.name # => "A"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With