2 ms·
The irritating problem with this is when two different pieces of code--such as two separate gems--both attempt to add the same method to the same class. Classes
by ajross 17y ago
The irritating problem with this is when two different pieces of code--such as two separate gems--both attempt to add the same method to the same class. Classes are global in Ruby, so we end up with a race condition in our code.
Obviously not the point of the article, but it's still important to get your jargon right. This isn't a race condition. It could be, if you were trying to modify the same class from separate threads. But the mere attempt to share access to a global variable is not a synchronization bug, and you shouldn't be using synchronization terms for it. This is usually just called a "namespace collision".
- raganwald 17y agoExcellent point, thanks! Although it feels like one in that the behaviour sometimes comes across as a Heisenbug: depending on the order the environment loads various gems and files, you can have a code that sometimes does one thing and sometimes another.
- aaronblohowiak 17y agoThis is also wrong. Classes aren't global in Ruby. Classes are just Objects. Constants have weird scoping issues. you can get into some really weird conditions when you start doing interesting metaprogramming. For instance, try .dup'ing a Class object whose implementation refers to another class via its name. You will get an error that the constant can't be resolved.