Skip to content

Instantly share code, notes, and snippets.

@okuramasafumi
Last active April 5, 2019 06:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save okuramasafumi/1785ea7113deb4b367c74b36399c4837 to your computer and use it in GitHub Desktop.
Save okuramasafumi/1785ea7113deb4b367c74b36399c4837 to your computer and use it in GitHub Desktop.
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