Search code examples
rubymruby

How does const_get work in mruby?


I use mruby 1.3.0 (2017-7-4) with build_config.rb:

MRuby::Build.new do |conf|
  if ENV['VisualStudioVersion'] || ENV['VSINSTALLDIR']
    toolchain :visualcpp
  else
    toolchain :gcc
  end
  enable_debug
  conf.gembox 'default'
  conf.gem :git => 'https://github.com/mattn/mruby-uv'
  conf.gem :git => 'https://github.com/mattn/mruby-http'
  conf.gem :git => 'https://github.com/iij/mruby-socket'
  conf.gem :git => 'https://github.com/luisbebop/mruby-polarssl.git'
  conf.gem :git => 'https://github.com/iij/mruby-digest'
  conf.gem :git => 'https://github.com/iij/mruby-pack'
  conf.gem :git => 'https://github.com/matsumoto-r/mruby-simplehttp.git'
  conf.gem :git => 'https://github.com/matsumotory/mruby-httprequest'
  conf.gem :git => 'https://github.com/iij/mruby-aws-s3.git'
end

MRuby::Build.new('host-debug') do |conf|
  if ENV['VisualStudioVersion'] || ENV['VSINSTALLDIR']
    toolchain :visualcpp
  else
      toolchain :gcc
  end

  enable_debug
  conf.gembox 'default'
  conf.cc.defines = %w(MRB_ENABLE_DEBUG_HOOK)
  conf.gem :core => "mruby-bin-debugger"
end

MRuby::Build.new('test') do |conf|
  if ENV['VisualStudioVersion'] || ENV['VSINSTALLDIR']
    toolchain :visualcpp
  else
    toolchain :gcc
  end
  enable_debug
  conf.enable_bintest
  conf.enable_test
  conf.gembox 'default'
end

I found const_get method different in mruby from in ruby. In ruby (2.4.0p0), Class.const_get('Fixnum') returns the constant Fixnum, while in mruby Class.const_get('Fixnum') results in error uninitialized constant Class::Fixnum (NameError).

Then, I tried another example: class Hoge; end; class Hoge::Fuga; end. In ruby, both Class.const_get('Hoge::Fuga') and Hoge.const_get('Fuga') give the constant Hoge::Fuga. In mruby, it is only Hoge.const_get('Fuga') that returns Hoge::Fuga.


Solution

  • mruby's Module#const_get works like 2nd argument(which is named inherit to search for superclasses) false in CRuby. If you use Object.const_get(:Integer) instead it should behave the same as what you expected in both mruby and CRuby.