This is a project that will build the mRuby source into a XCode framework. That framework can then be used to embed Ruby into an iOS application.
This project is a fork of the original ios-ruby-embedded project. It includes the following differences:
- It depends on a fork of embedded Ruby that includes support for integer division (which is not configurable in standard mRuby)
- It automatically includes several gems not added to the original project
- It has been updated to support newer Xcode environments, and includes support for Bitcode
For a complete example of using the product of the build check out MRubyiOSExample
This is not an attempt to make a bridge between Objective-C and Ruby, if you want that then check out the mobiruby project.
git clone git://github.com/WaveformDelta/ios-ruby-embedded.gitcd ios-ruby-embeddedgit submodule initgit submodule updaterake
After the above steps you should have a complete MRuby.framework framework
structure that is ready to use.
The framework includes builds for five separate targets:
ios-armv7ios-armv7sios-arm64ios-simulatorios-simulator-x86_64
The build also produces a host target that generates the mRuby developement
tools in the bin folder.
You can verify the build was successful by running bin/mirb:
mirb - Embeddable Interactive Ruby Shell
> print "Hello, world!"
Hello, world! => nil
> 1/2
=> 0
> 1.0/2
=> 0.5
> exit
Note that this demonstrates that integer division is working as expected.
You can clean up the build simply by using rake clean.
If you make modifications such as adding additional gems and get the project
into a state where the regular rake clean fails, manually delete the following
files:
custom.gemboxios_build_config.rb
and run the clean command again. The above files will be regenerated by the build process.
To install the framework in an XCode project follow these steps (these steps assume XCode 9.0.x):
- Select the top of the project on the left hand project display
- Select the "Build Phases" tab in the project details
- Click the + button under the "Link Binary With Libraries" dropdown
- Select "Add Other..." from the framework add popup
- Navigate to the MRuby.framework directory in the file browser and click "Open"
Assume you have the following simple Ruby script you want to execute:
puts "Hello world"
First you need to compile the script into byte code using the mrbc command
that can be found in the bin directory:
mrbc helloworld.rb
The output of that command is a bytecode file that can be run using the mruby
command found in the same bin directory. NB you currently need to cat all your
script files together before compiling them, using require doesn't work.
Once you have compiled your Ruby code and added it to your application bundle you can embed it in your app using something like the following code:
#include "mruby/mruby.h"
#include "mruby/mruby/proc.h"
#include "mruby/mruby/dump.h"
// ...
NSString *bcfile = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"helloworld.mrb"];
mrb_state *mrb = mrb_open();
FILE *fp = fopen([bcfile UTF8String], "rb");
if (fp == NULL) {
NSLog(@"Error loading file...");
} else {
int n = mrb_read_irep_file(mrb, fp);
fclose(fp);
mrb_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_top_self(mrb));
}
- mruby is new and changing constantly, don't be surprised if this project doesn't build.
- Currently you can't use the mruby compiler while embedded in an arm7 device. That is why you need to pre-compile the code using mrbc. It will work on the simulator but don't let that fool you into thinking it will work on a device. An app created to compile code would almost certainly have other issues.
MIT to match the mruby license. See the LICENSE file for full license.