Ruby on Rails:将参数传递给单例

发布于 2024-08-13 10:46:18 字数 656 浏览 2 评论 0原文

我有一个 Rails 应用程序,它通过包装器反复与另一个 Web 服务器通信,我想将包装器放在 Singleton 类中,这样就不会为每个请求重新创建它。很简单,我想:

class AppWrapper < Wrapper
  include Singleton
end
...
wrapper = AppWrapper.instance "url"

只是它不起作用:

wrong number of arguments (0 for 1)
/usr/lib/ruby/1.8/singleton.rb:94:in `initialize'
/usr/lib/ruby/1.8/singleton.rb:94:in `new'
/usr/lib/ruby/1.8/singleton.rb:94:in `instance'

Wrapper.initialize 需要一个参数,并且显然它没有被传递,因为有问题的第 94 行说

@__instance__ = new # look Ma, no argument

我如何解决这个问题?在 AppWrapper 中重新定义初始化似乎没有帮助,并且 使用 Wrapper 将“set URL”与“initialize”分开似乎不是最理想的。

I have a Rails app that repeatedly talks to another Web server through a wrapper, and I'd like to stick the wrapper in a Singleton class so it's not recreated for every request. Easy enough, I thought:

class AppWrapper < Wrapper
  include Singleton
end
...
wrapper = AppWrapper.instance "url"

Only it doesn't work:

wrong number of arguments (0 for 1)
/usr/lib/ruby/1.8/singleton.rb:94:in `initialize'
/usr/lib/ruby/1.8/singleton.rb:94:in `new'
/usr/lib/ruby/1.8/singleton.rb:94:in `instance'

Wrapper.initialize needs an argument, and apparently it's not getting passed through, since line 94 in question says

@__instance__ = new # look Ma, no argument

How do I work around this? Redefining initialize in AppWrapper doesn't seem to help, and
mucking around with Wrapper to separate "set URL" from "initialize" seems suboptimal.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

眼泪也成诗 2024-08-20 10:46:18

将参数传递给单例

class Parameterized_Singleton

def initialize(a)
  @pdf = a
  puts @pdf
end

def self.instance(p)

  begin
    @@instance  =Parameterized_Singleton.new(p)
    private_class_method :new
    rescue NoMethodError
     # return @@instance  # or you can return previous object
      puts "Object Already Created"
      exit
  end

  return @@instance
end

def scanwith(b)
  puts "scan"
end

def show_frequence_distribution
  puts "fd"
end

def show_object_number(a)
  puts "no"
end


end


Parameterized_Singleton.instance(20).show_object_number(10)
Parameterized_Singleton.instance(10).show_object_number(20)

Passing argument to singleton

class Parameterized_Singleton

def initialize(a)
  @pdf = a
  puts @pdf
end

def self.instance(p)

  begin
    @@instance  =Parameterized_Singleton.new(p)
    private_class_method :new
    rescue NoMethodError
     # return @@instance  # or you can return previous object
      puts "Object Already Created"
      exit
  end

  return @@instance
end

def scanwith(b)
  puts "scan"
end

def show_frequence_distribution
  puts "fd"
end

def show_object_number(a)
  puts "no"
end


end


Parameterized_Singleton.instance(20).show_object_number(10)
Parameterized_Singleton.instance(10).show_object_number(20)
第几種人 2024-08-20 10:46:18

你确定你需要一个单身人士而不是一个工厂吗?请参阅

Are you sure you need a singleton and not a factory . Refer this

月亮坠入山谷 2024-08-20 10:46:18

当我还在了解 Ruby 的时候我就问了这个问题,现在看来很天真。简单的解决方案是将 Wrapper 对象存储在成员变量中,并仅在尚未设置时使用 ||= 对其进行初始化:

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

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
伴我老 2024-08-20 10:46:18

既然您提到了编辑 Wrapper 作为解决方案,那么您不能直接使用 Wrapper 并执行此操作吗?

class Wrapper; include Singleton; end

如果没有,您可以使用类似的方法,这将确保 AppWrapper.new 不会被多次调用:

class AppWrapper
  def self.new(*args)
    class << app_wrapper = Wrapper.new(*args)
      include Singleton
    end
    app_wrapper
  end
end

如果您需要单例“Klass.instance”方法,则必须取出参数Wrapper#initialize,或者只是重新定义 Singleton#instance 以选择性地接受参数并将它们传递给第 94 行的 new 调用。

Since you mention something about editing Wrapper as a solution, can't you just use Wrapper directly and do this?

class Wrapper; include Singleton; end

If not, you could use something like this, which will just make sure AppWrapper.new isn't called more than once:

class AppWrapper
  def self.new(*args)
    class << app_wrapper = Wrapper.new(*args)
      include Singleton
    end
    app_wrapper
  end
end

If you need the singleton "Klass.instance" method, you'll have to take either take out the parameter in Wrapper#initialize, or just redefine Singleton#instance to take arguments optionally and passes them to the call to new on line 94.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文