I have the code for this form:
class String
def is_jpg?
start_with? "\xFF\xD8\xFF".b
end
end
String # b returns a copy of the string, but is encoded as ASCII-8BIT. My question
Is the Ruby interpreter smart enough to cache the result of b () in the above example, so that calls to this method for different instances of String should not create a new copy of "\ xFF \ xD8 \ xFF" every time? Or should I do something like:
class String
JPG_SIGNATURE = "\xFF\xD8\xFF".b
def is_jpg?
start_with? JPG_SIGNATURE
end
end
The answer depends on the version of ruby and / or the interpreter? I am using MRI 2.2.x soon to be 2.3.x.
source
share