- 編輯器設定 soft tab (space=2),以 2 格空白符號做為程式內縮距離(不分語言)。
- 函式如果只有一個參數,就不強制打()
- 函式如果有二個以上的參數,通通都要有 ()
- (避免發生奇怪的paser bug跟保持專案一致性)
- 字串限定用雙引號包覆
- 善用 "#{str1} #{str3} " 等字串改寫技巧取代不需要的字串加法。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require'open-uri' | |
require'cgi' | |
module Bing | |
#doc:http://msdn.microsoft.com/zh-cn/library/dd251020(en-us).aspx | |
APP_ID = '0DDB7DBE7BABC4505C688F4E954FBBB4458403D8' | |
API_HOST ='http://api.bing.net/json.aspx' | |
def search(query, option={'AppId' => APP_ID, 'Market' => 'en-US', 'Sources' =>'web','Version' => '2.0', 'Web.Count'=>'10'}) | |
option['Query'] = query | |
url = API_HOST << '?' << option.map{|key,value| "#{key}=#{CGI.escape(value)}"}.join("&") | |
open(url).read |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#从纯真IP数据库里查询 | |
def search_ip_cz(ip) | |
#IP数据文件路径 | |
#ip='202.38.64.10' | |
dat_path = "QQWry.Dat" | |
#检查IP地址 | |
# if not ip~=/^d{1,3}.d{1,3}.d{1,3}.d{1,3}$/ | |
# return 'IP Address Error' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'find' | |
puts "input your dir:" | |
dir = gets.chomp | |
puts "input your pattern:" | |
pattern = gets.chomp | |
Find.find(dir) do |file| | |
puts file if File.file?(file) && File.basename(file) =~/#{pattern}/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Stack | |
def initialize | |
@store = [] | |
end | |
def push(x) | |
@store.push x | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def age | |
years = Date.today.year - birth.year | |
years + (Date.today < birth + years.year ? -1 : 0) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>> require 'htmlentities' | |
=> [] | |
>> coder = HTMLEntities.new | |
=> #<HTMLEntities:0x4903770 @flavor="xhtml1"> | |
=> "http://xx.com/店铺管理首页.aspx"75;.asp" | |
>> coder.decode(_).to_cn | |
http://xx.com/店铺管理首页.aspx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#utf-8 | |
def encode(str) | |
keys = "abcdefghijklmnopqrstuvwxyz234567" | |
str.unpack("C*").map{|b| "%08b" % b}.join.scan(/\d{1,5}/).map{|x| "0" * 3 + x.ljust(5,"0")}.map{|y| keys[y.to_i(2)].chr}.join | |
end | |
p encode "aaa" #=> "mfqwc" | |
p encode "JavaEye论坛频道" #=>"jjqxmykfpfs6rlv24wozx2ncshuydey" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=begin | |
<span class="next"> | |
<a href="/group/douban_radio/discussion?start=525">后页></a> | |
</span> | |
http://www.douban.com/group/douban_radio/discussion?start=0 | |
=end | |
require 'open-uri' | |
require 'hpricot' | |
module Douban |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'hpricot' | |
require 'open-uri' | |
require 'iconv' | |
url = "http://www.douban.com/group/yly/discussion?start=" | |
(11..60).each do |i| | |
sleep 1 | |
html = open(url + i.to_s) | |
doc = Hpricot.parse html | |
names = doc.search("div.article//tr/td[2]/a/*") |
OlderNewer