Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Weird rubygems error by redefining `find` method
module Enumerable
undef find
def find(ifnone=nil)
return to_enum(:find, ifnone) unless block_given?
each do |item|
return item if yield item
end
end
end

Here, with broken_find.rb required, specification.rb raises an error saying stub is an Array.

/Users/okuramasafumi/.rubies/ruby-2.6.1/lib/ruby/site_ruby/2.6.0/rubygems/specification.rb:1065:in find_active_stub_by_path': undefined method this' for #<Array:0x00007fc3e18ff960> (NoMethodError)

The error disappears with working_find.rb required.

The only difference is whether ifnone.call if ifnone exists or not.

However, in specification.rb uses find method without ifnone argument. This means ifnone is nil.

So, why adding ifnone.call if ifnone to broken_find.rb actually fixes the problem?

# From rubygems
def self.find_active_stub_by_path(path)
stub = @@active_stub_with_requirable_file[path] ||= (stubs.find { |s|
s.activated? and s.contains_requirable_file? path
} || NOT_FOUND)
stub.this
end
module Enumerable
undef find
def find(ifnone=nil)
return to_enum(:find, ifnone) unless block_given?
each do |item|
return item if yield item
end
ifnone.call if ifnone
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.