本站首页    管理页面    写新日志    退出


«August 2025»
12
3456789
10111213141516
17181920212223
24252627282930
31


公告
 本博客在此声明所有文章均为转摘,只做资料收集使用。

我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:
日志总数:1304
评论数量:2242
留言数量:5
访问次数:7593531
建立时间:2006年5月29日




[Ruby on Rails]Rails 1.2新特性体验(String和Array)
软件技术

lhwork 发表于 2007/1/19 16:01:34

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   上面是字符串部分,下面是数组部分:


阅读全文(1757) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.621 second(s), page refreshed 144753032 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号