a = 'a’
a[0…0] = ''
sock.puts(String.new(a)) # CRASH!
puts ‘We never get here.’
----- END CODE -----
Just fill in your favorite ip address and port.
I don’t know if this is a bug in Array[]=, or in String.new (which I
doubt, but if you just use ‘a’ instead of ‘String.new(a)’, it doesn’t crash)
or a TCPSocket problem, but you have to use all of these for it to crash.
If it matters, I am using the 1.7.2 windows build from rubycentral.com:
a = ‘a’
a[0…0] = ‘’
sock.puts(String.new(a)) # CRASH!
puts ‘We never get here.’
String.new wants a string (literal or otherwise) as its argument.
a is an Array, so it doesn’t work:
irb(main):003:0> a = [0…0]
[0…0]
irb(main):004:0> puts String.new(a)
TypeError: failed to convert Array into String
from (irb):4:in initialize' from (irb):4:in new’
from (irb):4
This is a case where, if you wanted, you could use #to_str, which
is called in cases where objects need to masquerade as strings:
irb(main):005:0> def a.to_str; to_s; end
nil
irb(main):006:0> puts String.new(a)
0…0