我正在尝试使用Mashape拼写检查API https://www.mashape.com/montanaflynn/spellcheck。我这样做的方式是,我创建了一个类,每当我想要使用拼写检查API时,它都会被初始化。每次我启动它并调用我在那个类中编写的方法时,我都会收到这样的错误消息,check_spell.rb:19:in 'run_script': uninitialized constant Spell_check::Mashape (NameError),我做错了什么?
可执行文件如下所示: testSpel
#!/usr/bin/env ruby
require_relative './lib/check_url'
require_relative './lib/extract_tags'
require_relative './lib/check_spell'
def instruction
<<EOS
# Enter the url/file to be checked
# Enter 'Q' or 'q' to exit
# Enter 'Help' for usage information
EOS
end
def menu
puts instruction
url = gets.chomp
controller(url)
end
def controller(url)
if url == "q" || url == "Q"
puts "Bye!"
elsif url == ""
puts "Invalid url"
menu
else
check_url(url)
end
end
def check_url(url)
if url.length > 0
test_url = Url_check.new(url)
if test_url.validate == 1
check_tags = Grab_text.new(url)
text = check_tags.print_p
check = Spell_check.new(text)
check.run_script
else
puts "#{url} is not a valid url. Please enter a valid url"
puts menu
end
end
end
menulib/check_spell.rb
require 'rubygems'
require 'unirest'
require 'open-uri'
require 'nokogiri'
require 'uri'
class Spell_check
def initialize(sentence)
@sentence = sentence
end
def run_script
response = Unirest.get "https://montanaflynn-spellcheck.p.mashape.com/check/?text=This+sentnce+has+some+probblems.", headers:{:X-Mashape-Key => "yPE6iL0onCmshTi11F0PM89eYtjGp1xxxeBjsnppk45Zxxx"}
puts response
end
end 发布于 2014-08-20 23:28:15
我建议你编辑你的问题,从代码中删除你的X-Mashape-Key,因为这可能会被用来对付你。其次,因为Spell_check位于另一个模块中,所以在调用它时需要使用全名表示法。
希望这能帮上忙
最好的
https://stackoverflow.com/questions/25394842
复制相似问题