大佬教程收集整理的这篇文章主要介绍了ruby – 在编写宝石时设置配置设置,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个配置类允许配置gem:
@H_394_4@module Namechecker class Configuration attr_accessor :api_key,:log_level def initialize self.api_key = nil self.log_level = 'info' end end class << self attr_accessor :configuration end def self.configure self.configuration ||= Configuration.new yield(configuration) if block_given? end end现在可以这样使用:
Namechecker.configure do |config| config.api_key = 'dfskljkf' end
但是,我似乎无法使用我的gem中的其他类访问我的配置变量.例如,当我在我的spec_Helper.rb中配置gem时,如下所示:
# spec/spec_Helper.rb require "name_checker" Namechecker.configure do |config| config.api_key = 'dfskljkf' end
并从我的代码引用配置:
# lib/name_checker/net_checker.rb module Namechecker class Netchecker p Namechecker.configuration.api_key end end
我得到一个未定义的方法错误:
`<class:Netchecker>': undefined method `api_key' for nil:NilClass (NoMethodError)
我的代码有什么问题?
def self.configuration @configuration ||= Configuration.new end def self.configure yield(configuration) if block_given? end
以上是大佬教程为你收集整理的ruby – 在编写宝石时设置配置设置全部内容,希望文章能够帮你解决ruby – 在编写宝石时设置配置设置所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。