mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
Adds Module#< and Module#<=
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
class Module
|
||||
|
||||
##
|
||||
# call-seq:
|
||||
# mod < other -> true, false, or nil
|
||||
#
|
||||
# Returns true if <i>mod</i> is a subclass of <i>other</i>. 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.equal?(other)
|
||||
false
|
||||
else
|
||||
self <= other
|
||||
end
|
||||
end
|
||||
|
||||
##
|
||||
# call-seq:
|
||||
# mod <= other -> true, false, or nil
|
||||
#
|
||||
# Returns true if <i>mod</i> is a subclass of <i>other</i> or
|
||||
# is the same as <i>other</i>. 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
|
||||
|
||||
end
|
||||
@@ -1,3 +1,57 @@
|
||||
assert 'Module#<' do
|
||||
a = Class.new
|
||||
b = Class.new(a)
|
||||
c = Class.new(a)
|
||||
d = Module.new
|
||||
e = Class.new { include d }
|
||||
f = Module.new { include d }
|
||||
|
||||
# compare class to class
|
||||
assert_true b < a
|
||||
assert_false b < b
|
||||
assert_false a < b
|
||||
assert_nil c < b
|
||||
|
||||
# compare class to module
|
||||
assert_true e < d
|
||||
assert_false d < e
|
||||
assert_nil a < d
|
||||
|
||||
# compare module to module
|
||||
assert_true f < d
|
||||
assert_false f < f
|
||||
assert_false d < f
|
||||
|
||||
assert_raise(TypeError) { a < Object.new }
|
||||
end
|
||||
|
||||
assert 'Module#<=' do
|
||||
a = Class.new
|
||||
b = Class.new(a)
|
||||
c = Class.new(a)
|
||||
d = Module.new
|
||||
e = Class.new { include d }
|
||||
f = Module.new { include d }
|
||||
|
||||
# compare class to class
|
||||
assert_true b <= a
|
||||
assert_true b <= b
|
||||
assert_false a <= b
|
||||
assert_nil c <= b
|
||||
|
||||
# compare class to module
|
||||
assert_true e <= d
|
||||
assert_false d <= e
|
||||
assert_nil a <= d
|
||||
|
||||
# compare module to module
|
||||
assert_true f <= d
|
||||
assert_true f <= f
|
||||
assert_false d <= f
|
||||
|
||||
assert_raise(TypeError) { a <= Object.new }
|
||||
end
|
||||
|
||||
assert 'Module#name' do
|
||||
module Outer
|
||||
class Inner; end
|
||||
|
||||
Reference in New Issue
Block a user