Skip to content

Commit

Permalink
Implement Module#name.
Browse files Browse the repository at this point in the history
Solves #2132.
  • Loading branch information
take-cheeze authored and matz committed Nov 13, 2016
1 parent 88604e3 commit 451985d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
3 changes: 3 additions & 0 deletions mrbgems/default.gembox
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ MRuby::GemBox.new do |conf|
# Use Kernel module extension
conf.gem :core => "mruby-kernel-ext"

# Use extensional Module class
conf.gem :core => "mruby-module-ext"

# Use mruby-compiler to build other mrbgems
conf.gem :core => "mruby-compiler"
end
5 changes: 5 additions & 0 deletions mrbgems/mruby-module-ext/mrbgem.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
MRuby::Gem::Specification.new('mruby-module-ext') do |spec|
spec.license = 'MIT'
spec.author = 'mruby developers'
spec.summary = 'extensional Module class'
end
23 changes: 23 additions & 0 deletions mrbgems/mruby-module-ext/src/module.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "mruby.h"
#include "mruby/class.h"
#include "mruby/string.h"

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);
}

void
mrb_mruby_module_ext_gem_init(mrb_state *mrb)
{
struct RClass *mod = mrb->module_class;

mrb_define_method(mrb, mod, "name", mrb_mod_name, MRB_ARGS_NONE());
}

void
mrb_mruby_module_ext_gem_final(mrb_state *mrb)
{
}
10 changes: 10 additions & 0 deletions mrbgems/mruby-module-ext/test/module.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
assert 'Module#name' do
module A
class B
end
end

assert_nil A::B.singleton_class.name
assert_equal 'Fixnum', Fixnum.name
assert_equal 'A::B', A::B.name
end

0 comments on commit 451985d

Please sign in to comment.