Arup Rakshit
Arup Rakshit

Reputation: 118271

Confusion with string#concat(integer) method's output

I was playing around to test the String#concat(integer) method. The code is as follows:

irb(main):006:0> a="hello"
=> "hello"
irb(main):008:0> a<< "world"
=> "helloworld"
irb(main):009:0> a.concat(33)
=> "helloworld!"
irb(main):010:0> a.concat(32)
=> "helloworld! "
irb(main):011:0> a.concat(31)
=> "helloworld! \x1F"
irb(main):012:0> a.concat(34)
=> "helloworld! \x1F\""
irb(main):013:0> a.concat(3)
=> "helloworld! \x1F\"\x03"
irb(main):014:0>

But couldn't understand—why does 33 value give the helloworld! output (correctly, but not the other integers)?

Why/How does 32 give the output "helloworld! "?

In what way a << "world" made the string internally?

Upvotes: 0

Views: 91

Answers (1)

Endre Simo
Endre Simo

Reputation: 11551

It gives you helloworld! because ASCII code for ! mark is 33. Link.

Upvotes: 1

Related Questions