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.

···

--
Posted via http://www.ruby-forum.com/.

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

Dear Shoaib Ahmed,

Following the answer of Arun Sharma...

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.

···

On Wed, Feb 5, 2014 at 5:08 PM, Arun kant sharma <iarunkant@gmail.com> wrote:

is probably you are looking for.

Arun Kant Sharma wrote in post #1135721:

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

Yes ARUN

···

--
Posted via http://www.ruby-forum.com/\.

Abinoam Jr. wrote in post #1135757:

Dear Shoaib Ahmed,

Following the answer of Arun Sharma...

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:

···

On Wed, Feb 5, 2014 at 5:08 PM, Arun kant sharma <iarunkant@gmail.com> > wrote:

--
Posted via http://www.ruby-forum.com/\.