Класс-синглетон


# Вариант 1 - "классический"

#!/usr/bin/ruby

# Singleton

class SingletonClass

    private_class_method :new

    @@obj = nil

    def SingletonClass.create
        @@obj = new unless @@obj
        @@obj
    end

end


x = SingletonClass.create
y = SingletonClass.create

puts x.object_id
puts y.object_id

Вариант 2 - "руби"

#!/usr/bin/ruby

# Singleton

class Singleton
    class << self
        def printName
            puts self.object_id
        end
    end
end

Singleton.printName