class String
def colorize(color_code)
"\e[#{color_code}m#{self}\e[0m"
end
end
The general advice is to avoid confusing others though, so changing base classes like this is not really a business-safe idea. class String
def colorize(color_code)
"\e[#{color_code}m#{self}\e[0m"
end
end
The general advice is to avoid confusing others though, so changing base classes like this is not really a business-safe idea.Trying to make such experiments safe in Ruby would be unwise, but starting them in Ruby is happiness.
The drawback in this case is it's not as concise as just typing in a String literal :)
But this is true of any language. The truth is in the middle ground.
In Java, for example, one can do static analysis and identify when a class is doing reflection... and flag as an error if the class is doing reflection on a class that is an instance of java.something.
Class cache = Integer.class.getDeclaredClasses()[0];
Field c = cache.getDeclaredField("cache");
c.setAccessible(true);
I can examine to see that java.lang.{something} class is getting reflected on. I can check to see if anything is calling getDeclaredField and flag that. I can see the setAccessible and really flag that.This isn't about coding errors. This is about being able to analyze the code and see if the code is using prohibited methods.
Through static analysis, I can verify if anyone is trying to do something tricky with the String class.
Can I verify if a method is being modified added to an instance of a String in Ruby through static analysis?