在群发文本应用中获取未定义的错误

时间:2015-11-02 13:32:41

标签: ruby json irb

我正在创建一个应用程序,该应用程序使用带有数字和名称的JSON文件发送大量文本。每当我在IRB中测试加载应用程序时,我都会收到错误:

NameError: undefined local variable or method `data_from_file' for main:Object
        from /home/qc/tep/Coding Stuff/Ruby/text app/main.rb:14:in `contacts_from_file'

我理解错误的含义,但我不明白为什么我会收到错误,这里是源代码:

require 'json'

def sanatize(numbers)
    "+1" + number.gsub(/^1|\D/, "")
end

def numbers_from_file
    file = open('numbers.json').read
    JSON.parse(file)
end

def contacts_from_file
    contacts= { }
    data_from_file['feed']['entry'].each do |entry|
        name = entry['gsx$name']['$t']
        number = entry['gsx$number']['$t']
        contacts[sanatize(number)] = name
    end
    contacts
end

def contact_numbers
    contacts_from_file.keys
end

def contact_name
    contacts_from_file[number]
end

这里是JSON文件:

{ 
    '1**********' => 'Big Bird'
    '1**********'} => 'Josh'
}

如果有人可以帮助我并告诉我为什么data_from_file是"未定义"这将非常有帮助,谢谢你提前。

1 个答案:

答案 0 :(得分:0)

您永远不会定义<ol> <li> Pellentesque sed placerat nunc. Maecenas dignissim nec sapien sed ultricies. Nullam faucibus dolor nec enim scelerisque tristique. Aliqtae convallis lacus, nec mollis metus. mauris, et dictum nibh cursus sed. Integer feugiat augue sit amet enim vehicula, id vulputate leo lacinia. </li> <li> Pellentesque sed placerat nunc. Maecenasas. Vam tempus tincidunt lobortis nec quam. Interdum et malesuada fames ac ante ipsum primis in faucibus. Vivamus nec dolor semper, lacinia justo nec, pellentesque nisl. Nullam imperdiet convallis mauris, et dictum nibh cursus sed. Integer feugiat augue sit amet enim vehicula, id vulputate leo lacinia. </li> </ol> ,只是尝试使用data_from_file方法阅读它。

也许你的意思是contects_from_file而不是numbers_from_file