Home > Blockchain >  Ruby break line in multiple string interpolation
Ruby break line in multiple string interpolation

Time:11-23

I want to add below 5 strings using the interpolation. Each string should be separated by a new line:

ts = (Time.now.getutc.to_f * 1000).to_i.to_s
nonce = SecureRandom.uuid
host = 'example.com'
resource_path = '/v3/workflows/auth_flow/test'
body_hash = { some: 'hash' }.to_s

To do so I'm trying to:

signature = "#{ts}\n#{nonce}\n#{host}\n#{resource_path}\n#{body_hash}"

But I'm not sure this is correct way because what I getting is:

2.7.0 :105 > signature
 => "1637625282656\n666256a1-656c-49ed-b816-47211aecb4b4\nexample.com\n/v3/workflows/auth_flow/test\n{:some=>\"hash\"}"

Doesn't this just add an extra string "\n" instead of a newline after each string?

CodePudding user response:

Your code is correct and this is the expected output. IRB calls inspect method on each object it's trying to display as a return value. For string it means wrapping in double quotes and escaping special characters, including "\n". You can "force" it to display the string as is with puts signature.

In case of actual \n as two character long string, it would read "\\n"

  •  Tags:  
  • ruby
  • Related