Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit beef835

Browse files
committed
Remove const_defined? workaround
1 parent 2184664 commit beef835

File tree

1 file changed

+15
-40
lines changed

1 file changed

+15
-40
lines changed

lib/rspec/support/recursive_const_methods.rb

Lines changed: 15 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,52 +5,27 @@ module Support
55
module RecursiveConstMethods
66
# We only want to consider constants that are defined directly on a
77
# particular module, and not include top-level/inherited constants.
8-
# Unfortunately, the constant API changed between 1.8 and 1.9, so
9-
# we need to conditionally define methods to ignore the top-level/inherited
10-
# constants.
118
#
12-
# Given:
139
# class A; B = 1; end
1410
# class C < A; end
1511
#
16-
# On 1.8:
17-
# - C.const_get("Hash") # => ::Hash
18-
# - C.const_defined?("Hash") # => false
19-
# - C.constants # => ["B"]
20-
# - None of these methods accept the extra `inherit` argument
21-
# On 1.9:
22-
# - C.const_get("Hash") # => ::Hash
23-
# - C.const_defined?("Hash") # => true
24-
# - C.const_get("Hash", false) # => raises NameError
25-
# - C.const_defined?("Hash", false) # => false
26-
# - C.constants # => [:B]
27-
# - C.constants(false) #=> []
28-
if Module.method(:const_defined?).arity == 1
29-
def const_defined_on?(mod, const_name)
30-
mod.const_defined?(const_name)
31-
end
32-
33-
def get_const_defined_on(mod, const_name)
34-
return mod.const_get(const_name) if const_defined_on?(mod, const_name)
35-
36-
raise NameError, "uninitialized constant #{mod.name}::#{const_name}"
37-
end
38-
39-
def constants_defined_on(mod)
40-
mod.constants.select { |c| const_defined_on?(mod, c) }
41-
end
42-
else
43-
def const_defined_on?(mod, const_name)
44-
mod.const_defined?(const_name, false)
45-
end
12+
# C.const_get("Hash") # => ::Hash
13+
# C.const_defined?("Hash") # => true
14+
# C.const_get("Hash", false) # => raises NameError
15+
# C.const_defined?("Hash", false) # => false
16+
# C.constants # => [:B]
17+
# C.constants(false) #=> []
18+
19+
def const_defined_on?(mod, const_name)
20+
mod.const_defined?(const_name, false)
21+
end
4622

47-
def get_const_defined_on(mod, const_name)
48-
mod.const_get(const_name, false)
49-
end
23+
def get_const_defined_on(mod, const_name)
24+
mod.const_get(const_name, false)
25+
end
5026

51-
def constants_defined_on(mod)
52-
mod.constants(false)
53-
end
27+
def constants_defined_on(mod)
28+
mod.constants(false)
5429
end
5530

5631
def recursive_const_get(const_name)

0 commit comments

Comments
 (0)