Skip to content

Instantly share code, notes, and snippets.

@masamitsu-murase
Created September 11, 2012 17:30
Show Gist options
  • Save masamitsu-murase/3700054 to your computer and use it in GitHub Desktop.
Save masamitsu-murase/3700054 to your computer and use it in GitHub Desktop.
mruby with NPAPI plugin
<script id="ruby_script" type="text/ruby">
#<![CDATA[
num = 0
1000.times do |i|
num += i
end
hash = { result: num, input: $input } # $input has JavaScript function.
hash[:input].call({ a: num }) # JS function is called.
$result = hash[:result]
# ]]>
</script>
<script type="text/javascript">
// Global variable $input is shared in mruby and JavaScript.
$input = function(i){
add("test: " + i.a); // i is JS object converted from mruby hash.
};
// Add div element.
function add(str)
{
var elem = document.createElement("div");
document.getElementById("result").appendChild(elem);
var text_elem = document.createTextNode(str);
elem.appendChild(text_elem);
}
</script>
<form>
<!-- 'ruby(id)' method runs mruby script in script tag. -->
<input type=button value="test" onclick='alert(ruby("ruby_script"))' />
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment