今回は、大げさな機能ではないですが、 知っているとちょっと便利なHelper関数を紹介します。
cycle
まずはcycle。これはCSSのクラスにeven
, odd
を指定する場合のような、
周期的に繰り返す文字列を簡単に埋め込むことが出来ます。
1 @items = [1,2,3,4] 2 <table> 3 <% @items.each do |item| %> 4 <tr class="<%= cycle("even", "odd") -%>"> 5 <td>item</td> 6 </tr> 7 <% end %> 8 </table>
pluralize
英語のみですが、数詞の語尾変化を適切に行ってくれます。
1 pluralize(1, 'person') # => 1 person 2 pluralize(2, 'person') # => 2 people 3 pluralize(3, 'person', 'users') # => 3 users 4 pluralize(0, 'person') # => 0 people
posted by
genki
on Mon 3 Mar 2008
at 15:29