Skip to content

Instantly share code, notes, and snippets.

@sivagao
sivagao / Ruby: testing@ruby best practices.rb
Created November 9, 2012 06:10
Ruby Best Practices: Testing (mocking,stubbing, check output, iteration of)
if foo ! = 'blah'
puts "i expected 'blah' but foo contains #{{foo}}"
end
require "test/unit"
class MyThingieTest < Test::Unit::TestCase
def test_must_be_empty
@sivagao
sivagao / Ruby: using json.rb
Created November 9, 2012 10:47
using json @ ruby. from ruby best practices
require "json"
hash = { "Foo" => [Math::PI, 1, "kittens"],
"Bar" => [false, nil, true], "Baz" => { "X" => "Y" } } #...
puts hash.to_json #=> Outputs
{"Foo":[3.14159265358979,1,"kittens"],"Bar":[false,null,true],"Baz":{"X":"Y"}}
require "json"
json_string = '{"Foo":[3.14159265358979,1,"kittens"], ' +
'"Bar":[false,null,true],"Baz":{"X":"Y"}}'
@sivagao
sivagao / using four kinds argument & block.rb
Created November 9, 2012 10:50
ruby best practices. four kind arguments[ordinal, optional arugment, pseudo-keyword argument, arguments as an Array] and block
# Standard ordinal arguments
def distance(x1,y1,x2,y2)
Math.hypot(x2 - x1, y2 - y1)
end
# Ordinal arguments, with an optional argument
def load_file(name,mode="rb")
File.open(name,mode)
end
# Pseudo-keyword arguments
@sivagao
sivagao / Ruby: ruby best practices, ruby worst practices.rb
Created November 9, 2012 10:58
ruby best practices, ruby worst practices, harmful class variables. hardcoding into a corner, inheritance becomes restrictive, evails of evel(), resuce blind, method_missing
require "pstore"
# pay attention to the psstore;s usage
# @@data = PStore,new("filename.store")
# @@data[id] = <Ruby Object> should be surrounded by @@data.transition do <> end block
class User
def self.data
@data ||= PStore.new("users.store") #in class method, @data means the class instance variable.
end
@sivagao
sivagao / Ruby: mastering the dynamic toolkit.rb
Created November 9, 2012 16:39
ruby best practices,mastering the dynamic toolkit.
builder = Builder::XmlMarkup.new(:target=>STDOUT, :indent=>2)
builder.person { |b| b.name("Jim"); b.phone("555-1234") }
>> BasicObject.instance_methods
=> [:==, :equal?, :!, :!=, :instance_eval, :instance_exec, :__send__]
class BlankSlate
class << self
def hide(name)
if instance_methods.include?(name) and name !~ /^(_|instance_eavl)/
@sivagao
sivagao / Ruby: RR , flexmock testing doubles.rb
Created November 9, 2012 20:59
test doubles: mock, stub, proxy, spy, and so on . Ruby RR , flexmock
# RR
# RR (Double Ruby) is a test double framework that features a rich selection of double techniques and a terse syntax.
#
#
# one of the goal of RR is to make doubles more scannable.
# accomplished by making the double declartion look as much as the actual method invocation as possible
flexmock(User).should_receive(:find).with('42').and_return(jane) # Flexmock
User.should_receive(:find).with('42').and_return(jane) # Rspec
User.expects(:find).with('42').returns {jane} # Mocha
User.should_receive(:find).with('42') {jane} # Rspec using return value blocks
== 克隆周边 ==
在较老一代的版本控制系统里,checkout是获取文件的标准操作。你将获得一组特定保
存状态的文件。
在Git和其他分布式版本控制系统里,克隆是标准的操作。通过创建整个仓库的克隆来
获得文件。或者说,你实际上把整个中心服务器做了个镜像。凡是主仓库上能做的事,
你都能做。
=== 计算机间同步 ===
@sivagao
sivagao / gitmagic: secrets.txt
Created November 10, 2012 00:56
我们揭开Git神秘面纱,往里瞧瞧它是如何创造奇迹的。对象数据库,commit,tree,blob对象文件。索引,head等
== 揭开面纱 ==
我们揭开Git神秘面纱,往里瞧瞧它是如何创造奇迹的。我会跳过细节。更深入的描述参见 http://www.kernel.org/pub/software/scm/git/docs/user-manual.html[ 用户手册]。
=== 大象无形 ===
直到你需要它的时候,而且那是你欢欣的时候,Git一直默默注视着你。
Git简单地在你工作目录下的`.git`目录保存你项目的历史。
=== 数据完整性 ===
== 克隆周边 ==
在较老的版本控制系统中,checkout是获取文件的标准操作。你讲获取一组特定保存状态的文件。
在git等其他分布式控制系统中,clone是标准的操作。
=== 计算机间同步 ===
我可以忍受制作tar包或利用rsync来作备份和基本同步。但我有时在我笔记本上编辑,其他时间在台式机上,而且这俩之间也许并不交互。
@sivagao
sivagao / gitmagic: branches.txt
Created November 10, 2012 00:57
gitmagic: braches.txt
== 分支巫术 ==
即时分支合并时Git最给力的杀手锏
*方案* :Git有一个更好的工具对付这种情况,比克隆快多了而且节省空间: *git branch* 。
使用这个魔咒,目录里的文件突然从一个版本变到另一个。除了只是在历史记录里上跳下窜外,这个转换还可以做更多。你的文件可以从上一个发布版变到实验版本到当前开发版本到你朋友的版本等等。
=== 老板键 ===