Module: RubyBreaker::RubyTypeUtils

Defined in:
lib/rubybreaker/typing/rubytype.rb

Class Method Summary collapse

Class Method Details

.subclass_rel?(lhs, rhs) ⇒ Boolean

Checks if lhs is a subclass of rhs

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rubybreaker/typing/rubytype.rb', line 25

def self.subclass_rel?(lhs, rhs)
  return false unless lhs.kind_of?(Class)
  if lhs == rhs
    return true
  elsif lhs == BasicObject 
    # lhs != rhs and no more to look upward, so quit 
    return false
  elsif rhs.instance_of?(Module)
    # lhs != rhs and rhs is a module, so lhs must have a parent module 
    # that is a subclass of rhs
    lhs.included_modules.each {|m|
      return true if self.submodule_rel?(m,rhs)
    }
  else
    # then rhs is a class, so just go up as a class
    return self.subclass_rel?(lhs.superclass, rhs)
  end
  return false
end

.submodule_rel?(lhs, rhs) ⇒ Boolean

Checks if lhs is a sub-module of rhs

Returns:

  • (Boolean)


12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rubybreaker/typing/rubytype.rb', line 12

def self.submodule_rel?(lhs,rhs)
  # both lhs and rhs must be modules
  if lhs == rhs 
    return true
  else
    lhs.included_modules.each {|m|
      return true if self.submodule_rel?(m,rhs)
    }
  end 
  return false
end