Ruby

.underscore

1
2
3
'ActiveModel'.underscore         # => "active_model"

'ActiveModel::Errors'.underscore # => "active_model/errors"

string 使用

用於切換 檔名 與路徑

.fetch

1
2
3
4
5
h = { "a" => 100, "b" => 200 }
h.fetch("a")                            #=> 100
h.fetch("z", "go fish")                 #=> "go fish"

h.fetch("z") { |el| "go fish, #{el}"}   #=> "go fish, z"

hash 使用

抓出hash裡面的值 如果沒有就報錯 如果沒有但有給default 回報default 的值 如果沒有 但有定義後面的 code block 把搜尋的值 塞進去

Factroyfirl

sequence

1
2
3
4
5
FactoryGirl.define do
  sequence :email do |n|
    "person#{n}@example.com"
  end
end

用於產生序列化的變數 使用上要好於使用隨機數字-> 不會重複