Skip to content
Permalink
Browse files

Merge pull request #669 from krekoten/module_comparison

Module comparison methods
  • Loading branch information
alex committed Apr 30, 2013
2 parents 48a0ca3 + 03a1cae commit 4f93593a06262ae45fe80cb383f16d5711cdb4a6

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

@@ -516,6 +516,56 @@ def method_method_definedp(self, space, name):
def method_eqeqeq(self, space, w_obj):
return space.newbool(self.is_ancestor_of(space.getclass(w_obj)))

@classdef.method("<=")
def method_lte(self, space, w_other):
if not isinstance(w_other, W_ModuleObject):
raise space.error(space.w_TypeError, "compared with non class/module")
for w_mod in self.ancestors():
if w_other is w_mod:
return space.w_true
for w_mod in w_other.ancestors():
if self is w_mod:
return space.w_false
return space.w_nil

@classdef.method("<")
def method_lt(self, space, w_other):
if self is w_other:
return space.w_false
return space.send(self, space.newsymbol("<="), [w_other])

@classdef.method(">=")
def method_gte(self, space, w_other):
if not isinstance(w_other, W_ModuleObject):
raise space.error(space.w_TypeError, "compared with non class/module")
return space.send(w_other, space.newsymbol("<="), [self])

@classdef.method(">")
def method_gt(self, space, w_other):
if not isinstance(w_other, W_ModuleObject):
raise space.error(space.w_TypeError, "compared with non class/module")
if self is w_other:
return space.w_false
return space.send(w_other, space.newsymbol("<="), [self])

@classdef.method("<=>")
def method_comparison(self, space, w_other):
if not isinstance(w_other, W_ModuleObject):
return space.w_nil

if self is w_other:
return space.newint(0)

other_is_subclass = space.send(self, space.newsymbol("<"), [w_other])

if space.is_true(other_is_subclass):
return space.newint(-1)
elif other_is_subclass is space.w_nil:
return space.w_nil
else:
return space.newint(1)


@classdef.method("instance_method", name="symbol")
def method_instance_method(self, space, name):
return space.newmethod(name, self)

0 comments on commit 4f93593

Please sign in to comment.