2

我不断收到此错误:

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

我正在关注这个网站上的练习:

这是我的代码(我的错误以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 

我已经尝试了几件事,通过互联网检查了所有内容,但我自己无法弄清楚。

我将非常感谢任何帮助。

先感谢您!

4

1 回答 1

0

您有output_data嵌入方法的定义print_zipcodes(迭代块未关闭)。尝试缩进你的代码——这样就很容易发现错误。

于 2012-11-22T22:44:04.797 回答