Browse files

Be defensive about resolving `Object`

It'd be pretty crazy for there to be an `RSpec::Object` or
`RSpec::Expectations::Object,` but 1) Ruby's constant resolution rules
have changed and I don't always remember them and 2) We were originally
defensive about it.
  • Loading branch information...
1 parent 147f710 commit 3cacddebccae668a815241e7e9dc542e0f2f36bf @alindeman alindeman committed Jul 25, 2012
Showing with 1 addition and 1 deletion.
  1. +1 −1 lib/rspec/expectations/syntax.rb
2 lib/rspec/expectations/syntax.rb
@@ -40,7 +40,7 @@ module Syntax
# @api private
# Determines where we add `should` and `should_not`.
def default_should_host
- @default_should_host ||= Object.ancestors.last
+ @default_should_host ||= ::Object.ancestors.last
# @api private

2 comments on commit 3cacdde

RSpec member

Besides this being defensive, I think it's potentially faster. When you reference a top-level constant w/o prefixing it with ::, I think it first tries to find it each level of the module nesting, then looks at ancestors and finally finds it.

In contrast, :: tells it exactly where to look and skips some of those steps.

I haven't actually benchmarked it to compare, but based on my understanding of constants in ruby this would make sense.



Please sign in to comment.