ruby 代码
- >> "Finally, something useful!".at(6)
- => "y"
2 from 和 to (String)
- >> "Chris the Person".from(6)
- => "the Person"
- >> "Chris the Person".to(4)
- => "Chris"
3 first 和 last (String)
- >> "Christmas Time".first
- => "C"
- >> "Christmas Time".first(5)
- => "Chris"
- >> "Christmas Time".last
- => "e"
- >> "Christmas Time".last(4)
- => "Time"
4 each_char 把字符串转换成字符数组进行处理
5 starts_with? 和 ends_with?去检测一个regular expression
- >> "Peanut Butter".starts_with? 'Peanut'
- => true
- >> "Peanut Butter".ends_with? 'Nutter'
- => false
6 to_time 和 to_date
7 转换的方法:
pluralize, singularize, camelize, titleize, underscore, dasherize, demodulize, tableize, classify, humanize, foreign_key, and constantize
8 to_sentence把数组转换成一个字符串,用逗号分隔,并且在最后一个元素前加上'and'。可以带两个参数,:connector 和 :skip_last_comma.第一个参数最后一个元素前要加上的字符串,默认是‘and’。后一个表示是否在最后两个元素之间加上逗号分隔符。false:加上。true:不加上,默认是false
- >> %w[Chris Mark Steven].to_sentence
- => "Chris, Mark, and Steven"
- >> %w[Soap Mocha Chocolate].to_sentence(:connector => '&')
- => "Soap, Mocha, & Chocolate"
- >> %w[Ratatat Interpol Beirut].to_sentence(:skip_last_comma => true)
- => "Ratatat, Interpol and Beirut" <post></post>
- >> array_of_posts = Post.find(:all, :limit => 3)
- => [#
- >> array_of_posts.to_s(:db)
- => "1,2,3"
- >> [].to_s
- => ""
- >> [].to_s(:db)
- => "null"
11 to_xml,包含几个参数。
Pass in :skip_instruct to omit the line. You can also provide an :indent option (default: 2), a :builder option (you probably want to stick with the default of Builder::XmlMarkup), a :root option (defaults to the lowercase plural name of the class, posts in this case), and a children option (singular lowercase class—post for me).
- >> puts array_of_posts.to_xml
- => "?>
- <posts></posts>
- <post></post>
- ... tons of xml ...
12 in_groups_of (只能在 ActiveRecord objects中使用。 )
13 split