暇つぶしのPerl
深い意味はまったく無いんだけど,Code Snippetsで現在最新のエントリを勝手にPerlで書くことによって,単なる暇つぶしを試みるというお話.多分続かない.
今日のお題(一部修正済).
時刻を表す文字列を,コロンで分割して,秒数に直して返すメソッド?名前が嫌なのでcolon2secondsとかに変更していいですか.// converts colon-separated time to seconds // "01:02:03" => 3723 def colon_separated_to_i(str) time_arr = [] time_arr = str.split(":").reverse sec = 0 secs = [1, 60, 60*60, 60*60*24] for i in 0...time_arr.length sec += secs[i].to_i * time_arr[i].to_i end sec end
sub colon2seconds { return sub { $_[-3] * 3600 + $_[-2] * 60 + $_[-1] }->(split /:/, shift); }
Perlらしいキモさ炸裂だな.