Ruby新手:未定义的方法“具有不同的访问权限”
我是一名新的Ruby程序员,我的一位同事帮助我开始编写以下代码,这些代码在他的环境中运行良好。但是,当我尝试在自己的环境中运行它时,我发现了以下错误:对于NoMethodError,未定义方法“with_interference_access” 该方法在代码中出现两次:Ruby新手:未定义的方法“具有不同的访问权限”,ruby,rubygems,Ruby,Rubygems,我是一名新的Ruby程序员,我的一位同事帮助我开始编写以下代码,这些代码在他的环境中运行良好。但是,当我尝试在自己的环境中运行它时,我发现了以下错误:对于NoMethodError,未定义方法“with_interference_access” 该方法在代码中出现两次: require 'rubygems' gem 'activerecord' gem 'activesupport' gem 'sailthru-client' require 'active_support' require
require 'rubygems'
gem 'activerecord'
gem 'activesupport'
gem 'sailthru-client'
require 'active_support'
require 'active_record'
require 'sailthru'
# Setup our Sailthru object using our production Sailthru account information
sailthru = Sailthru::SailthruClient.new()
# Read database information from the database.yml file
CONFIG = YAML.load_file(File.join(File.dirname(__FILE__), 'database.yml')).with_indifferent_access
# Create a simple way for us to iterate through all publications
class Publication < ActiveRecord::Base
establish_connection CONFIG[:production]
set_table_name 'publications'
end
# Create a simple way for us to store data locally
class CurrentReport < ActiveRecord::Base
establish_connection CONFIG[:development]
set_table_name 'current_reports'
end
class MonthlyReport < ActiveRecord::Base
establish_connection CONFIG[:development]
set_table_name 'monthly_reports'
end
types = %w[Daily Weekly]
Publication.find_each(:select => :id) do |publication|
begin
types.each do |newsletter_type|
# Get the stats for each mailing list
response = sailthru.stats_list("#{newsletter_type} Newsletter - #{publication.id}").with_indifferent_access
if response[:error]
puts "Sailthru Error #{response[:error]}: #{response[:errormsg]}"
else
# Try to find an existing record for this newsletter
daily = CurrentReport.find_or_initialize_by_list(response[:list])
# and update it with the information from the response (minus the monthly signup info)
puts "Updating #{newsletter_type} Newsletter - #{publication.id} ..."
daily.update_attributes(response.reject { |k,v| k =~ /signup/ })
# Iterate through the monthly signup info
response['signup_month'].each do |k, v|
# And try to save it
monthly = MonthlyReport.find_or_initialize_by_list_and_month(response[:list], k)
# Only save new months, because the old months never change
if monthly.new_record?
monthly.update_attributes(v)
puts "\tAdding #{v[:month]} to #{response[:list]} ..."
end
end
end
end
# rescue NoMethodError => e
# puts "Got a NoMethodError for some reason. Here's the publication: #{publication.inspect}\n\nHere's the types array: #{types}"
end
end
我尝试过不同版本的ruby,比如ruby-1.8.7,但都没有用。我不知道如何解决这个问题。我知道这个方法存在于某个地方,因为我看到了它的工作原理。我愿意接受任何关于下一步尝试的建议。尝试使用:
require 'active_support/core_ext/hash'
这就是在普通散列类中添加with\u冷漠访问方法的原因。顺便说一句,HashWith冷漠访问非常强大,只要您谨慎使用它 例如:
h = HashWithIndifferentAccess.new()
some_array.each do |a|
h["#{a}"] = "anything you want"
end
在处理metrics中的数据片段时,我一直在使用它。感谢您的帮助。我认为它奏效了。对于所有这些,我都是新手,在哪里可以找到关于Ruby的在线文档,比如找到Hash类是哪个库的一部分?Hash是Ruby core的一部分,可以在这里找到哪些文档:至于像active_支持这样的扩展,我喜欢直接看源代码,在这种情况下,可以在这里找到:如果你真的只想要无所谓的访问,你可以在没有其他哈希扩展的情况下使用require“active\u support/core\u ext/hash/aniverse\u access”来要求它。这如何证明需要谨慎呢?