Ubuntu Server 16.04下uninitialized constant MRuby (NameError)错误求助
Hey there, let's tackle this uninitialized constant MRuby (NameError) issue step by step—since you're new to Ruby, I'll break things down clearly so you can follow along.
First: Understand the difference between CRuby and MRuby
First off, standard Ruby (CRuby) and MRuby are two separate Ruby runtimes—they don't share constants or contexts by default. That error usually pops up when you're trying to reference MRuby in the wrong environment:
- If you're running your code with the regular
rubycommand (CRuby), this constant doesn't exist here. MRuby is a lightweight, embedded version of Ruby, so you can't just call its constants from CRuby directly. - If you're running the code through H2O's MRuby handler, the issue is likely with how H2O was compiled or how your script is structured.
Step 1: Verify H2O was compiled with MRuby support
You mentioned compiling H2O 2.3 with Hiredis—double-check that you included MRuby in the configure step. Here's the correct command structure (adjust paths if your MRuby/Hiredis aren't in default locations):
./configure --with-mruby --with-hiredis=/path/to/your/hiredis-install make && sudo make install
- Make sure there were no errors during compilation. If you saw warnings about missing MRuby headers/libraries, recheck your MRuby installation path—MRuby needs to be compiled from source (not just installed via apt) for H2O to integrate it properly.
Step 2: Test MRuby standalone first
Let's rule out MRuby itself being broken:
- Navigate to your MRuby installation directory
- Run
bin/mrubyto launch the MRuby interactive shell - Type
puts "Hello from MRuby!"—if this runs without errors, MRuby is working. If not, recompile MRuby:cd /path/to/mruby-repo make clean make sudo make install
Step 3: Fix your MRuby script for H2O
If you're using the script as an H2O handler, you don't need to reference the MRuby constant directly. MRuby in H2O uses a specific handler syntax. Here's a minimal working example to replace your simplified code:
# Simple H2O MRuby handler that returns a 200 response proc do |env| [200, {}, ["Hello from MRuby + H2O!"]] end
Then update your H2O config to point to this script:
hosts: "your-server-ip": paths: "/": mruby.handler: /path/to/your/script.rb
Restart H2O and test the endpoint—this should work if your compilation was correct.
Step 4: Avoid mixing CRuby and MRuby contexts
If you accidentally ran your MRuby-focused script with ruby (CRuby), that's definitely why you're seeing the MRuby constant error. Stick to running MRuby scripts through H2O's handler or MRuby's own bin/mruby command.
内容的提问来源于stack exchange,提问作者James Lei

