# Class method call via the singleton or not

**URL:** <https://www.ruby-forum.com/t/class-method-call-via-the-singleton-or-not/76110>\
**Category:** Ruby\
**Created:** [November 16, 2006, 12:06am UTC](https://www.ruby-forum.com/t/class-method-call-via-the-singleton-or-not/76110 "2006-11-16T00:06:05Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [November 16, 2006, 12:06am UTC](https://www.ruby-forum.com/t/class-method-call-via-the-singleton-or-not/76110/1 "2006-11-16T00:06:05Z")

</div>

Hi–

Little bit of conundrum. I have a variables that tracks information  
about methods. So it needs to work within the context of the class  
hierarchy. At the same time I need to access that information from the  
instance level. I worked it out with one exception --singleton, b/c I  
do something like:

class Foo

```
def self.dolist( name )
  list = nil
  ancestors.each do |a|
    if d = a.instance_variable_get("@dolist")
      list = d[name.to_sym]
      break if list
    end
  end
  list
end

def foo
  self.class.dolist()
end

```

end

This works unless there is singleton in the hierarchy that has @dolist  
defined. But I don’t want to call directly on the singleton in foo in  
case the singleton doesn’t exist --I think that would be an exceedingly  
inefficient. Is there another way?

Thanks,  
T.
