Ruby - Create Singleton with Parameters

How can I mix in Singleton to create a class that accepts initialization parameters?

Singleton does not provide this functionality, but instead of using singleton you could write it by yourself

class MyLogger
@@singleton__instance__ = nil
@@singleton__mutex__ = Mutex.new

def self.instance(file_name)
return @@singleton__instance__ if @@singleton__instance__

@@singleton__mutex__.synchronize do
return @@singleton__instance__ if @@singleton__instance__

@@singleton__instance__ = new(file_name)
end
@@singleton__instance__
end

private

def initialize(file_name)
@file_name = file_name
end
private_class_method :new
end

It should work, but I did not tested the code.

This code forces you to use MyLogger.instance <file_name> or at least at the first call if you know it will be first time calling.

Ruby on Rails: Passing argument to singleton

I asked this question while I was still getting my head around Ruby, and it seems so naive now. The easy solution is to just store the Wrapper object in a member variable and use ||= to initialize it only if it hasn't been set yet:

class WrapperUserClass
def initialize
@wrapper = nil # Strictly speaking unnecessary, but it's a bit clearer this way
end

def wrapper
@wrapper ||= Wrapper.new(foobar)
end

def do_something
wrapper.booyakasha
end
end

a problem about singleton in ruby

Since it's a singleton there's no need for passing any parameters to initialize, because it will always return the same object.

If you want to change something on the class everytime you call it you could define a new method.

require 'singleton'
class AAA
attr :string , true
include Singleton
def initialize
@string = "aaa";
end
def self.change(string)
instance.string = string
instance
end
end
a = AAA.change("simpleton");
puts a.string

initializing ruby singleton

Rubymotion (1.24+) now seems to support using GCD for singleton creation

class MyClass
def self.instance
Dispatch.once { @instance ||= new }
@instance
end
end

How to allow multiple parameters to be passed into define_singleton_method

You're close:

def learn_maneuvering(name, &block)
define_singleton_method(name) do |*args|
args.each do |arg|
block.call(arg)
end
end
end

r = Robot.new
r.learn_maneuvering('turn') { |degree| puts "turning #{degree} degrees" }

r.turn 50, 60

prints:

turning 50 degrees

turning 60 degrees

But is this really what you want? It seems like just doing

r.turn 50
r.turn 60

makes more sense to me.

ruby: how to initialize class method with parameters?

You could do this.

class MyClass
singleton_class.send(:attr_accessor, :string_3)
end

MyClass.string_3 = "It's a fine day."
MyClass.string_3 #=> "It's a fine day."

@string_3 is a class instance variable.

How do I create a Class using the Singleton Design Pattern in Ruby?

Actually, the above answer was not completely correct.

require 'singleton'

class Example
include Singleton
end

You also need to include the require 'singleton' statement.



Related Topics



Leave a reply



Submit