1.each each_with_index each.with_index
['a', 'b', 'c'].each {|e| puts e} #=>'a' 'b' 'c'
['a', 'b', 'c'].each_with_index {|e,index| puts index} #=> 0 1 2
['a', 'b', 'c'].each.with_index(1) {|e,index| puts index} #=> 1 2 3
2.simple_format 将textarea中的/n换行格式化成br换行
<%= simple_format("foo\nbar") %>
# 输出 "<p>foo\n<br />bar</p>"
3.truncate 提取前几个字符
<%= truncate("Once upon a time in a world far far away") %>
# 输出 "Once upon a time in a world..."
<%= truncate("Once upon a time in a world far far away", length: 17) %>
# 输出 "Once upon a ti..."
4.strip_tags 移除HTML标籤
5.strip_links 移除HTML超连结标籤
6.distance_of_time_in_words 输出很潮的时间距离,例如
distance_of_time_in_words(Time.now, Time.now + 60.minutes)
=> "about 1 hour"
It looks fine, but we have some room to improve.
<abbr class="timeago" title="<%= comment.created_at.getutc.iso8601 %>">
<%= comment.created_at.to_s %>
</abbr>
here is a javascript solution based on jquery http://timeago.yarp.com/, of course, you can use other time ago js library.
$("abbr.timeago").timeago();
7.distance_of_time_in_words_to_now
distance_of_time_in_words_to_now(Time.now - 1.second)
=> "less than a minute"
8.time_tag 输出HTML5时间标籤
time_tag(Time.now)
=> "<time datetime=\"2014-11-03T23:55:11+08:00\">November 03, 2014 23:55</time>"
9.number_with_delimiter
number_with_delimiter(1234567)
=> "1,234,567"
10.number_with_precision
number_with_precision(123.4567, precision: 2)
=> "123.46"
11.number_to_currency
number_to_currency(111, unit: '¥')
=> ¥111.00