GithubHelp home page GithubHelp logo

takeru / ios-ruby-embedded Goto Github PK

View Code? Open in Web Editor NEW

This project forked from carsonmcdonald/ios-ruby-embedded

1.0 2.0 0.0 165 KB

Builds mruby into a XCode framework that can be used to embed ruby in your iOS app.

License: MIT License

Ruby 100.00%

ios-ruby-embedded's Introduction

About

This is a project that will build the current mruby source into a XCode framework. That framework can then be used to embed Ruby into an iOS application.

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.

Build

  • git clone git://github.com/carsonmcdonald/ios-ruby-embedded.git
  • cd ios-ruby-embedded
  • git submodule init
  • git submodule update
  • rake

After the above steps you should have a complete MRuby.framework framework structure that is ready to use.

Install

To install the framework in an XCode project follow these steps (these steps assume XCode 5.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 direcotry in the file browser and click "Open"

Example Use

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 after you have compiled the project:

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

Notes

  • 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.

License

MIT to match the mruby license. See the LICENSE file for full license.

ios-ruby-embedded's People

Contributors

carsonmcdonald avatar tosik avatar takeru avatar

Stargazers

 avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.