BasicObject help

I need help with one question i am not able to figure out what it
exactly is can anyone help me with that and explain me if possible.

Question : Given that SUPERCLASS returns nil when called on BasicObject
but a non-nil value otherwise,write a ruby method that if passed any
object will print the objects class and its ancestor classes all the way
up to BasicObject.

obj.class.ancestors
is probably you are looking for.

Dear Shoaib Ahmed,

Following the answer of Arun S…

On Wed, Feb 5, 2014 at 5:08 PM, Arun kant sharma [email protected]
wrote:

obj.class.ancestors

or

obj.class.ancestors - obj.class.included_modules

But if you are just willing a help at constructing the loop to iterate
through each superclass until it gets a nil…

The code will be something like this.

def superclasses(object)
super_classes = []
current_class = object.class
super_classes.push current_class
while current_class.superclass # When on BasicObject it will return
nil breaking the loop
current_class = current_class.superclass
super_classes.push current_class
end
super_classes
end

Writing it with while one line syntax

def superclasses(object)
super_classes = []
current_class = object.class
super_classes.push current_class
super_classes.push current_class while current_class =
current_class.superclass
super_classes
end

Best regards,
Abinoam Jr.

Arun Kant Sharma wrote in post #1135721:

obj.class.ancestors
is probably you are looking for.

Yes ARUN

Abinoam Jr. wrote in post #1135757:

Dear Shoaib Ahmed,

Following the answer of Arun S…

On Wed, Feb 5, 2014 at 5:08 PM, Arun kant sharma [email protected]
wrote:

obj.class.ancestors

or

obj.class.ancestors - obj.class.included_modules

But if you are just willing a help at constructing the loop to iterate
through each superclass until it gets a nil…

The code will be something like this.

def superclasses(object)
super_classes = []
current_class = object.class
super_classes.push current_class
while current_class.superclass # When on BasicObject it will return
nil breaking the loop
current_class = current_class.superclass
super_classes.push current_class
end
super_classes
end

Abinoam Jr. Thank you so much :slight_smile:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs