mruby / mruby

Lightweight Ruby
MIT License
5.31k stars 791 forks source link

uninitialized constant Regexp (NameError) #5850

Closed fab1an2 closed 2 years ago

fab1an2 commented 2 years ago

How I can init Regexp? This is normal default mruby 3.1.0 configuration without modifications.

mruby -e " puts 'test'.split(//).join('#') "
-e:1: uninitialized constant Regexp (NameError)

I call from mruby

include <mruby/re.h>

.... wynik = mrb_funcall(mrb, mrb_top_self(mrb), "napis", 1, mrb_str_new_cstr(mrb, "żółw"));

I get empty string if (!mrb_undef_p(wynik)) { mrb_print_error(mrb); }

trace (most recent call last):
    [1] (unknown):0
(unknown):0:in brr: uninitialized constant Regexp (NameError)

my mruby code

def napis(e)
 return e.split(//).join(' ')
end
dearblue commented 2 years ago

There is no Regexp in mruby itself. However, if Regexp is provided by a third party, there is a mechanism to include it at build time so that it can be used.

If you look at https://github.com/mruby/mgem-list, you will see multiple regular expression GEMs.

If you want to use a GEM that uses the onigmo library, which is a regular expression library used in CRuby, for example, put the following in your build configuration file.

# myconf.rb

MRuby::Build.new do |conf|
  conf.toolchain
  conf.gembox "default"
  conf.gem mgem: "mruby-onig-regexp"
end

Build with this and you can use regular expressions.

% rake MRUBY_CONFIG=myconf.rb clean all
...snip...

% bin/mruby -e " puts 'test'.split(//).join('#') "
t#e#s#t
matz commented 2 years ago

For most of the cases for embedded programming, we don't use Regexp, so it's intentionally optional. As @dearblue explained, link regexp mrbgem explicitly.