Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Yup, here's how to use it to define a Singleton that is transparent to the caller:

    class S
      class << self
        attr_accessor :singleton
      end
      
      attr_accessor :value
      
      def initialize
        @value = "xxx"
        S.singleton = self
        def S.new
          S.singleton
        end
      end  
    end
    
    a = S.new
    # => #<S:0x007ff4a41532e0 @value="xxx">
    b = S.new
    #=> #<S:0x007ff4a41532e0 @value="xxx">
    a.value = "zzz"
    b.value # "zzz"
     
    a.object_id == b.object_id
    #=> true
That said... doing this may win you great sorrow.


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: