Recipe 1.2. Substituting Variables into Strings
Problem
创建一个包含变量或者表达式的字符串
Solution
在一个字符串里面,用一个"#"前缀,并用"{}"来包含一个变量或者表达式.
number = 5
"The number is #{number}." # => "The number is 5."
"The number is #{5}." # => "The number is 5."
"The number after #{number} is #{number.next}."
# => "The number after 5 is 6."
"The number prior to #{number} is #{number-1}."
# => "The number prior to 5 is 4."
"We're ##{number}!" # => "We're #5!"
Discussion
但你用两个"创建了一个字符串,Ruby就会去检查是否有需要替换的代码.通常的情况下,可能你都不会去考虑的一种情况,当你的字符串里面
包含了"/n",Ruby就会去用一个换行符去替代.
Ruby支持更加复杂的字符串替代.任何在 #{}里面的代码,Ruby都会认为是一个表达式.这个表达式产生的结果就会作为这个字符串的一部分
替换进去.如果这个表达式的结果不是一个字符串,Ruby就是调用它的to_s方法,然后再替换.
用这样一个方式产生一个字符串,看上去很难区分它是否采用了这个一个替换的特性:
"#{number}" == '5' # => true
你可以使用这样的替换特性来在一个字符串中包含一大块代码.这个极端的例子定义了一个类在一个字符串里面;它调用了返回了这个类的
一个方法,我们的这个字符串就包含了这个返回值.你不应该有任何的理由来这样做,我们这样做只是表现一下这个特性的强大.
%{Here is #{class InstantClass
def bar
"some text"
end
end
InstantClass.new.bar
}.}
# => "Here is some text."
在上面这个例子里面定义的类,和在其它任何地方定义的类是一样的,你可以像其它类一样使用类InstantClass,即使是在这个字符串外部
也可以.
如果一个字符串的替代机制里面调用了一个有副作用的函数,这个副作用就被触发.如果一个字符串里面定义了一个变量,这个变量在后面
也是可以访问的.依赖于这种行为是一个不好的方式,你需要有这样的意识:
"I've set x to #{x = 5; x += 1}." # => "I've set x to 6."
x # => 6
如果想避免这种替代的机制,你可以使用在#前加一个转义字符或者使用单引号.
"/#{foo}" # => "/#{foo}"
'#{foo}' # => "/#{foo}"
"here document"这样的构造方式和%{}的构造方式是一样的,但是可读性更好一点.
你可以使用下面的方式创建一个多行的字符串,Ruby在某一行碰到指定的字符串就会认为是字符串的结尾(不包含该指定的字符串):
name = "Mr. Lorum"
email = <<END
Dear #{name},
Unfortunately we cannot process your insurance claim at this
time. This is because we are a bakery, not an insurance company.
Signed,
Nil, Null, and None
Bakers to Her Majesty the Singleton
END
注:END必须在一行的开头,否则编译不过.
Ruby对于字符串的处理是非常灵活的,你可以用这样的方法来结束一个字符串("here document"):
<<end_of_poem
There once was a man from Peru
Whose limericks stopped on line two
end_of_poem
# => "There once was a man from Peru/nWhose limericks stopped on line two/n"
See Also
你可以使用Recipe 1.3介绍的方法来定义一个template string or object,并且用一个变量来替代.
Problem
创建一个包含变量或者表达式的字符串
Solution
在一个字符串里面,用一个"#"前缀,并用"{}"来包含一个变量或者表达式.
number = 5
"The number is #{number}." # => "The number is 5."
"The number is #{5}." # => "The number is 5."
"The number after #{number} is #{number.next}."
# => "The number after 5 is 6."
"The number prior to #{number} is #{number-1}."
# => "The number prior to 5 is 4."
"We're ##{number}!" # => "We're #5!"
Discussion
但你用两个"创建了一个字符串,Ruby就会去检查是否有需要替换的代码.通常的情况下,可能你都不会去考虑的一种情况,当你的字符串里面
包含了"/n",Ruby就会去用一个换行符去替代.
Ruby支持更加复杂的字符串替代.任何在 #{}里面的代码,Ruby都会认为是一个表达式.这个表达式产生的结果就会作为这个字符串的一部分
替换进去.如果这个表达式的结果不是一个字符串,Ruby就是调用它的to_s方法,然后再替换.
用这样一个方式产生一个字符串,看上去很难区分它是否采用了这个一个替换的特性:
"#{number}" == '5' # => true
你可以使用这样的替换特性来在一个字符串中包含一大块代码.这个极端的例子定义了一个类在一个字符串里面;它调用了返回了这个类的
一个方法,我们的这个字符串就包含了这个返回值.你不应该有任何的理由来这样做,我们这样做只是表现一下这个特性的强大.
%{Here is #{class InstantClass
def bar
"some text"
end
end
InstantClass.new.bar
}.}
# => "Here is some text."
在上面这个例子里面定义的类,和在其它任何地方定义的类是一样的,你可以像其它类一样使用类InstantClass,即使是在这个字符串外部
也可以.
如果一个字符串的替代机制里面调用了一个有副作用的函数,这个副作用就被触发.如果一个字符串里面定义了一个变量,这个变量在后面
也是可以访问的.依赖于这种行为是一个不好的方式,你需要有这样的意识:
"I've set x to #{x = 5; x += 1}." # => "I've set x to 6."
x # => 6
如果想避免这种替代的机制,你可以使用在#前加一个转义字符或者使用单引号.
"/#{foo}" # => "/#{foo}"
'#{foo}' # => "/#{foo}"
"here document"这样的构造方式和%{}的构造方式是一样的,但是可读性更好一点.
你可以使用下面的方式创建一个多行的字符串,Ruby在某一行碰到指定的字符串就会认为是字符串的结尾(不包含该指定的字符串):
name = "Mr. Lorum"
email = <<END
Dear #{name},
Unfortunately we cannot process your insurance claim at this
time. This is because we are a bakery, not an insurance company.
Signed,
Nil, Null, and None
Bakers to Her Majesty the Singleton
END
注:END必须在一行的开头,否则编译不过.
Ruby对于字符串的处理是非常灵活的,你可以用这样的方法来结束一个字符串("here document"):
<<end_of_poem
There once was a man from Peru
Whose limericks stopped on line two
end_of_poem
# => "There once was a man from Peru/nWhose limericks stopped on line two/n"
See Also
你可以使用Recipe 1.3介绍的方法来定义一个template string or object,并且用一个变量来替代.