technorati.rb
来自「用ruby on rails写的一个博客程序,还不错..ruby on rail」· RB 代码 · 共 52 行
RB
52 行
require 'open-uri'require 'time'require 'rexml/document'class Technorati include REXML def choose(num) return cosmos unless cosmos.size > num bag = [] set = cosmos.dup num.times {|x| bag << set.delete_at(rand(set.size))} bag end attr_accessor :url, :link, :title, :cosmos # This object holds given information of a picture class Inbound < Struct.new(:link, :title) def to_s; title end end def initialize(url, refresh = true) self.cosmos = [] self.url = url self.refresh if refresh end # This method lets you refresh the tasks int the tasks array # useful if you keep the object cached in memory and def refresh open(@url) do |http| parse(http.read) end endprivate def parse(body) xml = Document.new(body) self.cosmos = [] XPath.each(xml, "//item/") do |elem| inbound = Inbound.new inbound.title = XPath.match(elem, "title/text()").to_s inbound.link = XPath.match(elem, "link/text()").to_s cosmos << inbound end endend
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?