From e471d37ca5f1422860a1eaa81d4c9f1b3c8b6aed Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Thu, 30 Aug 2018 16:07:59 +0900 Subject: Separate meta-programming features to `mruby-metaprog` gem. We assume meta-programming is less used in embedded environments. We have moved following methods: * Kernel module global_variables, local_variables, singleton_class, instance_variables, instance_variables_defined?, instance_variable_get, instance_variable_set, methods, private_methods, public_methods, protected_methods, singleton_methods, define_singleton_methods * Module class class_variables, class_variables_defined?, class_variable_get, class_variable_set, remove_class_variable, included_modules, instance_methods, remove_method, method_removed, constants * Module class methods constants, nesting Note: Following meta-programming methods are kept in the core: * Module class alias_method, undef_method, ancestors, const_defined?, const_get, const_set, remove_const, method_defined?, define_method * Toplevel object define_method `mruby-metaprog` gem is linked by default (specified in default.gembox). When it is removed, it will save 40KB (stripped:8KB) on x86-64 environment last time I measured. --- mrbgems/mruby-class-ext/test/module.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'mrbgems/mruby-class-ext') diff --git a/mrbgems/mruby-class-ext/test/module.rb b/mrbgems/mruby-class-ext/test/module.rb index ed6713aac..71a8da451 100644 --- a/mrbgems/mruby-class-ext/test/module.rb +++ b/mrbgems/mruby-class-ext/test/module.rb @@ -26,7 +26,7 @@ end assert 'Module#singleton_class?' do mod = Module.new cls = Class.new - scl = cls.singleton_class + scl = (class < Date: Sun, 2 Dec 2018 18:38:40 -0500 Subject: Adds Module#< and Module#<= --- mrbgems/mruby-class-ext/mrblib/module.rb | 39 +++++++++++++++++++++++ mrbgems/mruby-class-ext/test/module.rb | 54 ++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 mrbgems/mruby-class-ext/mrblib/module.rb (limited to 'mrbgems/mruby-class-ext') diff --git a/mrbgems/mruby-class-ext/mrblib/module.rb b/mrbgems/mruby-class-ext/mrblib/module.rb new file mode 100644 index 000000000..d35b4cbae --- /dev/null +++ b/mrbgems/mruby-class-ext/mrblib/module.rb @@ -0,0 +1,39 @@ +class Module + + ## + # call-seq: + # mod < other -> true, false, or nil + # + # Returns true if mod is a subclass 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 "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 mod is a subclass of other or + # is the same as 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 "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 diff --git a/mrbgems/mruby-class-ext/test/module.rb b/mrbgems/mruby-class-ext/test/module.rb index 71a8da451..52e04ab37 100644 --- a/mrbgems/mruby-class-ext/test/module.rb +++ b/mrbgems/mruby-class-ext/test/module.rb @@ -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 -- cgit v1.2.3 From 6ef4a5fd6b28375ec7c4f066cf8c5364256f83fd Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Tue, 4 Dec 2018 08:39:44 +0900 Subject: Replace RDoc `` to Markdown back quotes; ref #4174 --- mrbgems/mruby-class-ext/mrblib/module.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (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 d35b4cbae..484d63674 100644 --- a/mrbgems/mruby-class-ext/mrblib/module.rb +++ b/mrbgems/mruby-class-ext/mrblib/module.rb @@ -4,7 +4,7 @@ class Module # call-seq: # mod < other -> true, false, or nil # - # Returns true if mod is a subclass of other. Returns + # Returns true if `mod` is a subclass 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 "A < B".) @@ -22,8 +22,8 @@ class Module # call-seq: # mod <= other -> true, false, or nil # - # Returns true if mod is a subclass of other or - # is the same as other. Returns + # Returns true if `mod` is a subclass of `other` or + # is the same as `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 "A < B".) -- cgit v1.2.3 From d5d9cc8b62bd173375046d07fc62b75c3e2d807e Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Tue, 4 Dec 2018 08:40:23 +0900 Subject: Remove unnecessary check in `Module#<`; ref #4174 --- mrbgems/mruby-class-ext/mrblib/module.rb | 1 - 1 file changed, 1 deletion(-) (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 484d63674..8102b5417 100644 --- a/mrbgems/mruby-class-ext/mrblib/module.rb +++ b/mrbgems/mruby-class-ext/mrblib/module.rb @@ -10,7 +10,6 @@ class Module # "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 -- cgit v1.2.3 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 From faec8331b7805c810b83b94785323ed239a05cb7 Mon Sep 17 00:00:00 2001 From: KOBAYASHI Shuji Date: Mon, 17 Jun 2019 21:46:30 +0900 Subject: Remove unneeded `mrb_str_dup()` in `Module#name` `mrb_class_path()` always returns a new string or `nil`. --- mrbgems/mruby-class-ext/src/class.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'mrbgems/mruby-class-ext') diff --git a/mrbgems/mruby-class-ext/src/class.c b/mrbgems/mruby-class-ext/src/class.c index 705db9949..fdd56bcc3 100644 --- a/mrbgems/mruby-class-ext/src/class.c +++ b/mrbgems/mruby-class-ext/src/class.c @@ -5,8 +5,7 @@ static mrb_value mrb_mod_name(mrb_state *mrb, mrb_value self) { - mrb_value name = mrb_class_path(mrb, mrb_class_ptr(self)); - return mrb_nil_p(name)? name : mrb_str_dup(mrb, name); + return mrb_class_path(mrb, mrb_class_ptr(self)); } static mrb_value -- cgit v1.2.3