関数型呼び出しを排除せよ

せっかくオブジェクト指向言語使っているなら、オブジェクト指向で書こうよという話。

I try to always do in Ruby is avoid the functional style calls (i.e. time_month(month)), but since Ruby is so object-oriented, I try to extend or create classes/methods that allow for month.time_month type of calls.


つまりこういうこと。例えば、指定した数だけの文字を返すメソッドを書く場合

  • 関数型
def genarate_chars(length)
  "a" * length
end

generate_chars(5) #=> "aaaaa"
class Fixnum
  def chars
    "a" * self
  end
end

5.chars #=> "aaaaa"


後者の方が直感的でいい(*´艸`)