From e6bad6766a8ddc00c23b1c0204b047dfbf8e3041 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Tue, 4 Dec 2018 08:41:09 +0900 Subject: Add new methods `Module#{>,>=,<=>}`; ref #4174 --- mrbgems/mruby-class-ext/mrblib/module.rb | 51 ++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) (limited to 'mrbgems/mruby-class-ext') diff --git a/mrbgems/mruby-class-ext/mrblib/module.rb b/mrbgems/mruby-class-ext/mrblib/module.rb index 8102b5417..301585187 100644 --- a/mrbgems/mruby-class-ext/mrblib/module.rb +++ b/mrbgems/mruby-class-ext/mrblib/module.rb @@ -35,4 +35,55 @@ class Module end end + ## + # call-seq: + # mod > other -> true, false, or nil + # + # Returns true if `mod` is an ancestor of `other`. Returns + # nil 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 + # nil 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 -- cgit v1.2.3