← 全部文章

ROR开发小技巧

控制台示例

ruby-1.9.2-p180 :055 > File.join("lib/","ruby","gem.rb")
=> "lib/ruby/gem.rb"
ruby-1.9.2-p180 :058 > [1,2,3] - [1,2]
=> [3]
ruby-1.9.2-p180 :059 > [1,2,3] & [1,2]
=> [1, 2]
ruby-1.9.2-p180 :060 > [1,2,3] + [1,2,3,4]
=> [1, 2, 3, 1, 2, 3, 4]
ruby-1.9.2-p180 :066 > %w(a d c d 1 2 3)
=> ["a", "d", "c", "d", "1", "2", "3"]
ruby-1.9.2-p180 :074 > Hash["a",1,"b",2]
=> {"a"=>1, "b"=>2}
ruby-1.9.2-p180 :075 > Hash[*["a",1,"b",2]]
=> {"a"=>1, "b"=>2}
ruby-1.9.2-p180 :075 > Array("a".."z").select.with_index {|x,i| i%3 == 2}
=> ["c", "f", "i", "l", "o", "r", "u", "x"]

Unicode 与对象方法

# coding:utf-8
# unicode转义
"" << 0x6f22 #=> 漢
class Object
# returns instance methods of current object's class and its singleton methods
def lm # abbr of local methods
self.methods.sort - self.class.superclass.instance_methods
end
end

参考:

http://ruby-china.org/topics/541