用于#<eventmanager的未定义方法`output_data':0x007fa4220320c8>(NoMethodError)</eventmanager:0x007fa4220320c8>

时间:2012-11-22 22:36:52

标签: ruby class methods nomethoderror

我一直收到这个错误:

  

event_manager.rb:83:in':未定义的方法output_data' for #<EventManager:0x007fc5018320c0> (NoMethodError)

我正在进行练习on this website

这是我的代码(我的错误在DEF OUTPUT_DATA ...)结束时出现:

# Dependencies

require "csv"

# Class Definition

class EventManager

INVALID_PHONE_NUMBER = "0000000000"
INVALID_ZIPCODE = "00000"

def initialize
puts "EventManager Initialized."
filename = "event_attendees.csv"
@file = CSV.open(filename, {:headers => true, :header_converters => :symbol})
end

def print_names
@file.each do |line|
  puts line.inspect
  puts line[2] + " " + line[3]
 end
end

#printing home phone number method
def print_numbers
 @file.each do |line|
  number = clean_number(line[:homephone])
  puts number
end
end
 #cleaning numbers method
def clean_number(number)
  cleaner= number.delete('.' + ')' + '(' + '-')
  if cleaner.length == 10
    # Do Nothing
  elsif cleaner.length == 11
   if cleaner.start_with?("1")
    cleaner = cleaner[1..-1]
  else
    cleaner = INVALID_PHONE_NUMBER
  end
else
  cleaner = INVALID_PHONE_NUMBER
end
  return cleaner
end

def clean_zipcode(original)
    if original.nil?
    zipcode = INVALID_ZIPCODE
  elsif original.length < 5
    while original.length < 5
      original = original.insert(0, "0")
    end  
  else
  return original
end
return zipcode
end


def print_zipcodes
  @file.each do |line|
    zipcode = clean_zipcode(line[:zipcode])
    puts zipcode
end

def output_data
output = CSV.open("event_attendees_clean.csv", "w")
@file.each do |line|
  output << line
end
end
end
end





# Script
manager = EventManager.new
#manager.print_numbers
#manager.print_zipcodes
manager.output_data 

我已经尝试了几件事,通过互联网查看,我自己也无法弄明白。

我将非常感谢任何帮助。

提前谢谢!

1 个答案:

答案 0 :(得分:0)

您在output_data中嵌入了print_zipcodes方法的定义(迭代块未关闭)。尝试缩进代码 - 然后很容易发现错误。