- 編輯器設定 soft tab (space=2),以 2 格空白符號做為程式內縮距離(不分語言)。
- 函式如果只有一個參數,就不強制打()
- 函式如果有二個以上的參數,通通都要有 ()
- (避免發生奇怪的paser bug跟保持專案一致性)
- 字串限定用雙引號包覆
- 善用 "#{str1} #{str3} " 等字串改寫技巧取代不需要的字串加法。
This file contains hidden or 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 'hotcocoa' | |
require 'net/http' | |
# Replace the following code with your own hotcocoa code | |
class Application | |
include HotCocoa | |
This file contains hidden or 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
function random_chinese_name() { | |
function random(a, l) { | |
var x = []; | |
x.push(a[Math.ceil(Math.random() * a.length)]); | |
while(l > 1) { | |
x.push(a[Math.ceil(Math.random() * a.length)]); | |
l--; | |
} | |
return x.join(""); | |
} |
This file contains hidden or 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 String | |
# "Opportunity Type" => "opportunity_type" | |
# "Sub-segments" => "sub_segments" | |
def unhumanize | |
words = self.dup | |
words.downcase!.gsub!(/[ +|-]/, '_') | |
words | |
end | |
This file contains hidden or 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
# Eller's algorithm for maze generation. Novel in that it only requires memory | |
# proportional to the size of a single row; this means you can generate | |
# "bottomless" mazes with it, that just keep going and going and going, using | |
# constant memory! | |
class State | |
attr_reader :width | |
def initialize(width, next_set=-1) | |
@width = width |
This file contains hidden or 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
# Recursive backtracking algorithm for maze generation. Requires that | |
# the entire maze be stored in memory, but is quite fast, easy to | |
# learn and implement, and (with a few tweaks) gives fairly good mazes. | |
# Can also be customized in a variety of ways. | |
DIRS = (N, S, E, W = 1, 2, 4, 8) | |
DX = { E => 1, W => -1, N => 0, S => 0 } | |
DY = { E => 0, W => 0, N => -1, S => 1 } | |
OPPOSITE = { E => W, W => E, N => S, S => N } |
This file contains hidden or 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 'net/http' | |
http = Hash.new{|h,k| h[k] = Net::HTTP.get_response(URI(k)).body } | |
http['http://www.google.com'] # makes a request | |
http['http://www.google.com'] # returns cached value |
This file contains hidden or 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
# -------------------------------------------------------------------- | |
# An implementation of Kruskal's algorithm for generating mazes. | |
# Fairly expensive, memory-wise, as it requires memory proportional | |
# to the size of the entire maze, and it's not the fastest of the | |
# algorithms (what with all the set and edge management is has to | |
# do). Also, the mazes it generates tend to have a lot of very short | |
# dead-ends, giving the maze a kind of "spiky" look. | |
# -------------------------------------------------------------------- | |
# -------------------------------------------------------------------- |
This file contains hidden or 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 'OSM' | |
require 'OSM/StreamParser' | |
require 'OSM/Database' | |
class CustomCallback < OSM::Callbacks | |
def node(node) | |
@node = node |
This file contains hidden or 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
for i in {0..99}; do | |
wget `printf "http://downloads.cloudmade.com/north_america/north_america.osm.bz2.part.%02d" ${i};` | |
done |