So, I'm bored, and I've found what appears to be a strange inconsistency I was hoping to find more information on. This deals with string concatenation in Ruby, particularly appending what the string documentation refers to as a "codepoint".
Here're some examples:
'' << 233 #=> "é"
'' << 256 #=> "Ā"
Now, the curious thing is that in IRB both of those examples work. However, if you create a ruby class in a file, load the file, and execute the code, it blows up. See following example:
class MyConcatenationTest
def self.test
'' << 233
'' << 256
end
end
And then in IRB:
load 'my_concatenation_test.rb' #=> true
MyConcatenationTest.test #=> RangeError: 256 out of char range
So, my question is this: Why does this work in IRB, but not when I load a script that runs the same line of code?
Some other things to notice, if you alter the class:
class MyConcatenationTest
def self.test
'' << 233
#'' << 256
end
end
... and then reload/run the method, it returns the \x escaped value for 233 instead of the "é" from before:
load 'my_concatenation_test.rb'
MyConcatenationTest.test #=> "\xE9"
So... what's up with that? Both strings have the same encoding (UTF-8), and changing it to ASCII doesn't seem to make any difference.
EDIT: I should mention that I used 256 in the example above because that's the lowest number it blows up on. It's pretty obvious that it's freaking out because it can't properly deal with anything higher than "\xFF". To clarify my question, I'm curious to know why this limitation exists when the code exists in a loaded ruby file, but not in IRB.
puts RUBY_VERSIONto check that.