Ruby新手:未定义的方法`with_indifferent_access' [英] Ruby newbie: undefined method `with_indifferent_access'

查看:139
本文介绍了Ruby新手:未定义的方法`with_indifferent_access'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名新的Ruby程序员,并且是我的一位同事,帮助我开始编写下面的代码,这些代码在他的环境中运行良好.但是,当我尝试在自己的环境中运行它时,出现了以下错误:undefined method 'with_indifferent_access' for #<Hash:0x1012392c0>(NoMethodError)

I am a new Ruby programmer, and a co-worker of mine to help me get started wrote the following code which ran fine in his environment. However, when I try to run it in my own environment, I ge the follow error: undefined method 'with_indifferent_access' for #<Hash:0x1012392c0> (NoMethodError)

有问题的方法在代码中出现了两次:

The method in question appears twice in the code:

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,无济于事.我不知道如何解决这个问题.我知道此方法存在于某处,因为我已经看到它可以工作.我愿意接受任何有关下一步尝试的建议.

I have tried different versions of ruby, such as ruby-1.8.7, to no avail. I am at a loss on how to solve this problem. I know this method exists somewhere because I have seen it work. I am open to any suggestions on what to try next.

推荐答案

尝试使用:

require 'active_support/core_ext/hash'

这实际上是将with_indifferent_access方法添加到普通Hash类中的原因.

Thats what actually adds the with_indifferent_access method to the normal Hash class.

这篇关于Ruby新手:未定义的方法`with_indifferent_access'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆