Class: Module

Inherits:
Object show all
Defined in:
lib/ruby_us/extensions/module.rb

Instance Method Summary collapse

Instance Method Details

#deconstantizeObject



11
12
13
14
15
16
17
# File 'lib/ruby_us/extensions/module.rb', line 11

def deconstantize
  ancestor = ancestors.first
  splitted_trail = ancestor.to_s.split("::")
  trail_name = splitted_trail.slice(0, splitted_trail.length - 1).join("::")

  const_get(trail_name) if !trail_name.empty? && self.to_s != trail_name
end

#defines?(constant, verbose = false) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ruby_us/extensions/module.rb', line 19

def defines? constant, verbose=false
  splitted_trail = constant.split("::")
  trail_name = splitted_trail.first

  # try
  begin
    trail = const_get(trail_name) if Object.send(:const_defined?, trail_name)
    splitted_trail.slice(1, splitted_trail.length - 1).each do |constant_name|
      trail = trail.send(:const_defined?, constant_name) ? trail.const_get(constant_name) : nil
    end
    true if trail
  # catch (e)
  rescue Exception => e
    $stderr.puts "Exception recovered when trying to check if the constant \"#{constant}\" is defined: #{e}" if verbose
  end unless constant.empty?
end

#has_constants?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/ruby_us/extensions/module.rb', line 37

def has_constants?
  true if constants.any?
end