My project depends on Sunspot, which in turn depends on progress_bar. progress_bar defines a class (outside of any module/namespace) called ProgressBar
.
My test code depends on minitest-reporters, which in turn depends on ruby-progressbar. ruby-progressbar defines a class (also outside of any module/namespace) called ProgressBar
.
Being new to Ruby, it took me an embarassing amount of time to track down the fact that the two ProgressBar
classes were conflicting. I know how to namespace my own code to prevent such problems, but is there any way to resolve such a collision in 3rd party code?
Update:
The owner of minitest-reporters very kindly replaced his usage of the (apparently abandoned) ProgressBar with yet another progress library, PowerBar. So this addressed my problem. But guess what- PowerBar also pollutes the global namespace! This just seems pathological in Ruby...
If you're faced with
# alice_bad_code.rb
class ProgressBar
def initialize
puts "Alice bad code progress bar created"
end
end
class AliceBadCode
def initialize
@progress_bar = ProgressBar.new
end
end
and
# bob_bad_code.rb
class ProgressBar
def initialize
puts "Bob bad code progress bar created"
end
end
class BobBadCode
def initialize
@progress_bar = ProgressBar.new
end
end
Then I'd try something like
require "alice_bad_code"
class AliceBadCode
ProgressBar = ::ProgressBar
end
Object.send(:remove_const, "ProgressBar")
require "bob_bad_code"
class BobBadCode
ProgressBar = ::ProgressBar
end
Object.send(:remove_const, "ProgressBar") # Just in case
Welcome to Ruby metaprogramming!
However, the best way to monkey patch a third-party library is not to monkey patch it at all, and file a bug report as per Jorg W Mittag's answer.
There's really not more you can do.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With