joe*_*joe 3 ruby ruby-on-rails sinatra
我正在使用Ruby和Sinatra作为我的应用程序.
我想分配一个将在不同的类和方法中使用的变量.
在我的应用程序文件中,即'Millennium'是我的应用程序名称,因此应用程序文件millennium.rb包含:
require 'rubygems'
require 'sinatra'
require 'yaml'
require 'active_record'
require 'sidekiq'
require 'statsd'
custom_statsd = Statsd.new('localhost', 8125) #custom_statsd available across the application.
class Millennium < Sinatra::Application
set :use_queueing_in_dev, false # useful for debugging queue issues.
set :protection, :except => [:json_csrf]
configure do
# These allow our error handlers to capture the errors
disable :raise_errors
disable :show_exceptions
enable :logging
end
before do
#logger.info request.body.read
request.body.rewind
end
end
Run Code Online (Sandbox Code Playgroud)
在这里,我想custom_statsd在我的应用程序的任何类中使用变量的值.
我认为使用"$"不是一个好主意.请告诉我这是另一种方法.
谢谢!!!!
将实例放在共享配置模块中的类变量中可能稍微好一点,如下所示:
module MyAppConfig
def self.statsd
@statsd ||= Statsd.new('localhost', 8125)
end
end
class SomeOtherThing
def log!
MyAppConfig.statsd.something('hey')
end
end
SomeOtherThing.new.log!
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
500 次 |
| 最近记录: |