@@ -5,52 +5,27 @@ module Support
5
5
module RecursiveConstMethods
6
6
# We only want to consider constants that are defined directly on a
7
7
# 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.
11
8
#
12
- # Given:
13
9
# class A; B = 1; end
14
10
# class C < A; end
15
11
#
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
46
22
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
50
26
51
- def constants_defined_on ( mod )
52
- mod . constants ( false )
53
- end
27
+ def constants_defined_on ( mod )
28
+ mod . constants ( false )
54
29
end
55
30
56
31
def recursive_const_get ( const_name )
0 commit comments