ruby string escape when adding
In Ruby on Rails,I got a string number is made of 3 parts : prefix , counter , suffix
In model Setup:
def self.number
prefix = setup.receipt_prefix.blank? ? "" 开发者_如何学Go: setup.receipt_prefix.to_s
counter = setup.receipt_counter.blank? ? "" : setup.receipt_counter+1
suffix = setup.receipt_suffix.blank? ? "" : setup.receipt_suffix.to_s
each individual string shows fine:
puts prefix
=> \#_
puts counter
=> 1234
puts suffix
=> #$@s
but when I add 3 string together, an addition back slash appear :
prefix + counter + suffix
=> \\#_1234\#$@s
how can I escape "#" "\" when I add 3 string together ? like
=> \#_1234#$@s
any Ruby or Rails's helper I can use in the model? thx~~
The string will look different if you get the value versus print (puts) it out. See the following irb
session.
>> a = "\\#_"
=> "\\#_"
>> puts a
\#_
=> nil
>> b = "1234"
=> "1234"
>> puts a + b
\#_1234
=> nil
>> a + b
=> "\\#_1234"
The actual string value has two backslashes in it. But only one shows up if you print the string.
精彩评论