Ruby on Rails与Sinatra如何实现类似全局变量的长尾词功能?

2026-04-11 14:431阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计309个文字,预计阅读时间需要2分钟。

Ruby on Rails与Sinatra如何实现类似全局变量的长尾词功能?

我正在使用Ruby和Sinatra开发我的应用程序。我想在不同的类和方法中分配一个变量。在我的应用程序文件中,即Millennium是应用程序名称,因此应用程序文件名为millennium.rb。

我正在使用 Ruby和Sinatra作为我的应用程序.

我想分配一个将在不同的类和方法中使用的变量.

在我的应用程序文件中,即’Millennium’是我的应用程序名称,因此应用程序文件是millennium.rb包含:

Ruby on Rails与Sinatra如何实现类似全局变量的长尾词功能?

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

在这里,我想在我的应用程序的任何类中使用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!

本文共计309个文字,预计阅读时间需要2分钟。

Ruby on Rails与Sinatra如何实现类似全局变量的长尾词功能?

我正在使用Ruby和Sinatra开发我的应用程序。我想在不同的类和方法中分配一个变量。在我的应用程序文件中,即Millennium是应用程序名称,因此应用程序文件名为millennium.rb。

我正在使用 Ruby和Sinatra作为我的应用程序.

我想分配一个将在不同的类和方法中使用的变量.

在我的应用程序文件中,即’Millennium’是我的应用程序名称,因此应用程序文件是millennium.rb包含:

Ruby on Rails与Sinatra如何实现类似全局变量的长尾词功能?

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

在这里,我想在我的应用程序的任何类中使用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!