File: module.rb

package info (click to toggle)
mruby 3.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,584 kB
  • sloc: ansic: 51,933; ruby: 29,510; yacc: 7,077; cpp: 517; makefile: 51; sh: 42
file content (89 lines) | stat: -rw-r--r-- 2,414 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
class Module

  ##
  # call-seq:
  #   mod < other   ->  true, false, or nil
  #
  # Returns true if `mod` is a subclass of `other`. Returns
  # <code>nil</code> if there's no relationship between the two.
  # (Think of the relationship in terms of the class definition:
  # "class A < B" implies "A < B".)
  #
  def <(other)
    if self.equal?(other)
      false
    else
      self <= other
    end
  end

  ##
  # call-seq:
  #   mod <= other   ->  true, false, or nil
  #
  # Returns true if `mod` is a subclass of `other` or
  # is the same as `other`. Returns
  # <code>nil</code> if there's no relationship between the two.
  # (Think of the relationship in terms of the class definition:
  # "class A < B" implies "A < B".)
  def <=(other)
    raise TypeError, 'compared with non class/module' unless other.is_a?(Module)
    if self.ancestors.include?(other)
      return true
    elsif other.ancestors.include?(self)
      return false
    end
  end

  ##
  # call-seq:
  #  mod > other   ->  true, false, or nil
  #
  # Returns true if `mod` is an ancestor of `other`. Returns
  # <code>nil</code> if there's no relationship between the two.
  # (Think of the relationship in terms of the class definition:
  # "class A < B" implies "B > A".)
  #
  def >(other)
    if self.equal?(other)
      false
    else
      self >= other
    end
  end

  ##
  # call-seq:
  #   mod >= other   ->  true, false, or nil
  #
  # Returns true if `mod` is an ancestor of `other`, or the
  # two modules are the same. Returns
  # <code>nil</code> if there's no relationship between the two.
  # (Think of the relationship in terms of the class definition:
  # "class A < B" implies "B > A".)
  #
  def >=(other)
    raise TypeError, 'compared with non class/module' unless other.is_a?(Module)
    return other < self
  end

  ##
  # call-seq:
  #    module <=> other_module   -> -1, 0, +1, or nil
  #
  # Comparison---Returns -1, 0, +1 or nil depending on whether `module`
  # includes `other_module`, they are the same, or if `module` is included by
  # `other_module`.
  #
  # Returns `nil` if `module` has no relationship with `other_module`, if
  # `other_module` is not a module, or if the two values are incomparable.
  #
  def <=>(other)
    return 0 if self.equal?(other)
    return nil unless other.is_a?(Module)
    cmp = self < other
    return -1 if cmp
    return 1 unless cmp.nil?
    return nil
  end
end