【发布时间】:2011-06-14 08:38:14
【问题描述】:
我有一个使用 nokogiri 解析 XML 的 rails 3 模型中的方法。 如何在控制台中调用此方法以对其进行测试。
这是整个班级(我正在尝试调用 generate_list):
class Podcast < ActiveRecord::Base
validates_uniqueness_of :name
serialize :hosts
def generate_list
# fetch the top 300 podcasts from itunes
itunes_top_300 = Nokogiri.HTML(open("http://itunes.apple.com/us/rss/toppodcasts/limit=300/explicit=true/xml"))
# parse the returned xml
itunes_top_300.xpath('//feed/entry').map do |entry|
new_name = entry.xpath("./name").text
podcast = Podcast.find(:all, :conditions => {:name => new_name})
if podcast.nil?
podcast = Podcast.new(
:name => entry.xpath("./name").text,
:itunesurl => entry.xpath("./link/@href").text,
:category => entry.xpath("./category/@term").text,
:hosts => entry.xpath("./artist").text,
:description => entry.xpath("./summary").text,
:artwork => entry.xpath("./image[@height='170']").text
)
podcast.save
else
podcast.destroy
end
end
end
end
编辑:哇,1000 次观看。我希望这个问题对人们的帮助和对我的帮助一样多。当我回顾这一点时,我感到很惊讶,一年多以前,我无法弄清楚实例方法和类方法之间的区别。现在我正在用 ruby、Rails 和许多其他语言/框架编写复杂的面向服务的应用程序和后端。 堆栈溢出是造成这种情况的原因。非常感谢这个社区让人们能够解决他们的问题并理解他们的解决方案。
【问题讨论】:
-
请告诉我们这个方法是如何定义的。
标签: ruby-on-rails methods console