因此,我是ruby的新手,正在玩一個簡單的抓取腳本。我寫道:無法從另一個類方法調用正則表達式方法
class Scrape
def get_attribute(html, doc)
doc.css(html).to_s.strip.remove_html_tags
end
public
def remove_html_tags
re = /<("[^"]*"|'[^']*'|[^'">])*>/
self.gsub!(re, '')
end
end
有被排除的方法,但我跟着我的錯誤回到這個方法,我不斷收到以下每當get_attribute方法被稱爲:
NoMethodError: undefined method `remove_html_tags' for #<String:0x007fcf42fd5610>
唯一可行的事當我直接使用弦上GSUB:
def get_attribute(html, doc)
doc.css(html).to_s.strip.gsub(/<("[^"]*"|'[^']*'|[^'">])*>/, '')
end
我試過包括本remove_html_tags方法的模塊,以及,但似乎並沒有幫助。我找不到我失蹤的東西,任何幫助將不勝感激!
不需要「自己」... :-) – 2014-12-05 06:12:33
我試圖解釋的事情,而不是混淆代碼,以最小計數的字母使用。 – mudasobwa 2014-12-05 06:13:54
沒關係!:-) – 2014-12-05 06:15:16