class method call via the singleton or not

Discussion in 'Ruby' started by Trans, Nov 15, 2006.

  1. Trans

    Trans Guest

    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.
     
    Trans, Nov 15, 2006
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hal Fulton
    Replies:
    48
    Views:
    401
    Zach Dennis
    Sep 19, 2004
  2. Replies:
    6
    Views:
    109
    Trans
    Nov 7, 2005
  3. Trans
    Replies:
    6
    Views:
    100
    Trans
    Nov 19, 2006
  4. Paul McMahon
    Replies:
    3
    Views:
    210
    David A. Black
    Jun 9, 2008
  5. Charles Oliver Nutter

    Singleton methods without the singleton class

    Charles Oliver Nutter, Mar 15, 2010, in forum: Ruby
    Replies:
    4
    Views:
    212
    Charles Oliver Nutter
    Mar 22, 2010
Loading...

Share This Page