路线插值打印变量名而不是变量内容

问题描述 投票:0回答:1

我有这样一个类:

class Example
    def printThisVar(printThing)
        print 'this is the var: #{printThing}'   
    end
end

然而,被打印在打印该​​字符串"this is the var: #{printThing}""this is the var: exampleText"

有没有什么办法解决这一问题?谢谢!

ruby class string-interpolation
1个回答
3
投票

你有单引号,而不是要print串绕双引号。

class Example
    def print_this_var(print_thing)
        print 'this is the var: #{print_thing}'
    end
end

foo = Example.new
foo.print_this_var("example_text")

#=> this is the var: #{print_thing}

改变方法定义

        print "this is the var: #{print_thing}"

产量

#=> this is the var: example_text

串插不单引号工作。

© www.soinside.com 2019 - 2024. All rights reserved.