這是ActiveSupport::Inflector
String#dasherize
第一個參數:利用自身在紅寶石實例方法
def dasherize(underscored_word)
underscored_word.tr('_', '-')
end
它取代了與破折號的字符串下劃線。
'puni_puni'.dasherize # => "puni-puni"
接收器被用作該方法的唯一參數。
ActiveSupport::Inflector.dasherize(puni_puni) # => "puni-puni"
當我試圖做同樣的事情,這是行不通的:
module NewDash
def new_dasherize(underscored_word)
underscored_word.tr('_', '-')
end
end
String.include NewDash
t = "t_e_s_t"
t.new_dasherize # => ArgumentError: wrong number of arguments (0 for 1)
t.new_dasherize(t) # => "t-e-s-t"
我怎麼能複製這種行爲,什麼是它的技術術語?