Ruby newbie: undefined method with_indifferent_access

I am a new Ruby programmer, and my colleague wrote the following code to help me get started, which worked great in his environment. However, when I try to run it in my own environment, I will follow the following error: undefined method 'with_indifferent_access' for #<Hash:0x1012392c0> (NoMethodError)

This method 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 the publication: #{publication.inspect}\n\nHere the types array: #{types}" end end 

I tried different versions of ruby, such as ruby-1.8.7, to no avail. I do not understand how to solve this problem. I know this method exists somewhere because I saw how it works. I am open to any suggestions on what to do next.

+7
source share
2 answers

Try using:

 require 'active_support/core_ext/hash' 

What actually adds the with_indifferent_access method to the normal Hash class.

+24
source

By the way, HashWithIndifferentAccess is really powerful if you use it with caution.

For example:

 h = HashWithIndifferentAccess.new() some_array.each do |a| h["#{a}"] = "anything you want" end 

I use it all the time when working with pieces of data in metrics.

+1
source

All Articles