1 获取指定位置的字符ruby 代码
>> "Finally, something useful!".at(6)
=> "y" 2 from 和 to (String)
ruby 代码
>> "Chris the Person".from(6)
=> "the Person"
>> "Chris the Person".to(4)
=> "Chris" 3 first 和 last (String)ruby 代码>> "Christmas Time".first => "C" >> "Christmas Time".first(5) => "Chris" >> "Christmas Time".last => "e" >> "Christmas Time".last(4) => "Time" first带参数与to功能相似。last带参数与from功能相似4 each_char 把字符串转换成字符数组进行处理ruby 代码>> "Snow".each_char { |i| print i.upcase } SNOW >>"Snow".upcase SNOW 5 starts_with? 和 ends_with?去检测一个regular expressionruby 代码>> "Peanut Butter".starts_with? 'Peanut' => true >> "Peanut Butter".ends_with? 'Nutter' => false 6 to_time 和 to_dateruby 代码>> "1985-03-13".to_time => Wed Mar 13 00:00:00 UTC 1985 >> "1985-03-13".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
ruby 代码
>> %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"
10 rails重载了to_s方法。加入了一个参数:db,只能工作在ActiveRecord objects。输出所有model对象的id值。
ruby 代码
>> 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).ruby 代码>> puts array_of_posts.to_xml => "?> ... tons of xml ... 12 in_groups_of (只能在ActiveRecord objects中使用。)ruby 代码>> %w[1 2 3 4 5 6 7].in_groups_of(3) { |g| p g } ["1", "2", "3"] ["4", "5", "6"] ["7", nil, nil] >> %w[1 2 3].in_groups_of(2, ' ') { |g| p g } ["1", "2"] ["3", " "] >> %w[1 2 3].in_groups_of(2, false) { |g| p g } ["1", "2"] ["3"] >> %w[1 2 3 4 5 6 7].in_groups_of(3) => [["1", "2", "3"], ["4", "5", "6"], ["7", nil, nil]] 13 split ruby 代码 >> %w[Tom Jerry and Mickey and Pluto].split('and') => [["Tom", "Jerry"], ["Mickey"], ["Pluto"]] >> %w[Chris Mark Adam Tommy Martin Oliver].split { |name| name.first == 'M' } => [["Chris"], ["Adam", "Tommy"], ["Oliver"]]
ruby 代码
>> "reindeer".pluralize
=> "reindeers"
>> "elves".singularize
=> "elf"
>> "christmas_carol".camelize
=> "ChristmasCarol"
>> "christmas_carol".camelize(:lower)
=> "christmasCarol"
>> "holiday_cheer".titleize
=> "Holiday Cheer"
>> "AdventCalendar-2006".underscore
=> "advent_calendar_2006"
>> "santa_Claus".dasherize
=> "santa-Claus"
>> "Holiday::December::Christmas".demodulize
=> "Christmas"
>> "SnowStorm".tableize
=> "snow_storms"
>> "snow_storms".classify
=> "SnowStorm"
>> "present_id".humanize
=> "Present"
>> "Present".foreign_key
=> "present_id"
>> "Cheer".constantize
NameError: uninitialized constant Cheer
>> "Christmas".constantize
=> Christmas 上面是字符串部分,下面是数组部分: |