开发者

ruby string escape when adding

开发者 https://www.devze.com 2023-01-15 23:46 出处:网络
In Ruby on Rails,Igot a string number is made of 3 parts : prefix , counter , suffix In model Setup: def self.number

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号