欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

字符串操作

程序员文章站 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

 

相关标签: Blog