rails string相关转换

变量的读取

instance_variable_get("@name") #返回@name的值  

method相关

string到method名

send

book.send('name')
book.send("name=","test") ### book.name => test

string单数化singularize

"posts".singularize #=> "post"

table和class相关

# 将表格名转成class名
# classify
"blog_pictures".classify #=> 'BlogPicture' 

# class名转table name
"BlogPicture".tableize #=> "blog_pictures"

# 将class名转class
”post“.constantize #=> Post
"posts".classify.constantize.find_by_name("test") 

  

 

 

posted @ 2022-01-25 15:04  鞋带松了  阅读(15)  评论(0编辑  收藏  举报