字符串操作
程序员文章站
2022-04-05 10:19:19
...
@name = "hello"
# 返回@name的值
puts instance_variable_get("@name") # => hello
puts @name # => hello
# 设置@name的值 @name = "world"
puts instance_variable_set("@name", "world") # => world
puts @name # => world
space = Space.find(params[:id])
# 设置space.nickname的值 space.nickname = "jack"
space.send("nickname=","jack")
# 返回space.nickname的值
puts space.send("nickname") # => jack
# string复数化
puts "country".pluralize # => countries
# string单数化
puts "posts".singularize # => post
# 将表名转成class名
puts "blog_pictures".classify # => BlogPicture
# 将class名转成表名
puts "BlogPicture".tableize # => blog_pictures
# 将class名转成class
puts "Space".constantize # => Space
# 可以将classify和constantize联用,将string转成class model
puts "spaces".classify.constantize.find(params[:id]).user_type # => admin
上一篇: 获取document对象值总结
下一篇: 自己写的一个弹出层插件