`

ruby 问题总结

阅读更多

1.routes collection 和member的区别

例:
resources :photos do member do get :preview endend
resources :photos do collection do get :preview endend
结果
member /photos/1/preview preview_photo_path(photo)Acts on a specific resource so required id (preview specific photo)
collection /photos/preview preview_photos_url Acts on collection of resources(display all photos)
2.require 和 load的区别
require 只加载一次,load加载多次,load每次加载到该文件的时候执行改文件,require只在第一次执行是载入,以后碰到时自动忽略。require如果为ruby代码,可以省略后缀 。require将所有加载的文件保存在$变量中。
require ,load用于包含文件,include则用于包含的模块。
require一般用于加载库文件,load一般用于加载配置文件
3.join和spilt
join array to string
arr = ["hello", "world", 123]
puts arr.join(", ")
puts arr.join
hello, world, 123
helloworld123
spilt string to array
" now's  the time".split        #=> ["now's", "the", "time"]
" now's  the time".split('')   #=> ["now's", "the", "time"]
" now's  the time".split(//)   #=> ["", "now's", "", "the", "time"]
"1, 2.34,56, 7".split(%r{,\s*}) #=> ["1", "2.34", "56", "7"]
"hello".split(//)               #=> ["h", "e", "l", "l", "o"]
"hello".split(//, 3)            #=> ["h", "e", "llo"]
"hi mom".split(%r{\s*})         #=> ["h", "i", "m", "o", "m"]

"mellow yellow".split("ello")   #=> ["m", "w y", "w"]
"1,2,,3,4,,".split(',')         #=> ["1", "2", "", "3", "4"]
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics