Skip to content

Instantly share code, notes, and snippets.

@znz
Created February 23, 2013 13:30
Show Gist options
  • Save znz/5019741 to your computer and use it in GitHub Desktop.
Save znz/5019741 to your computer and use it in GitHub Desktop.
あとで書く

使い方

  1. http://doc.ruby-lang.org/ja/1.9.3/class/Array.html の冒頭に追加
  <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
  <script src="pre-to-mruby.js"></script>
  <script src="http://qiezi.me/projects/mruby-web-irb/mruby.js"></script>
  1. pre をクリック
  2. Eval をクリック
(function () {
var lines = [], printed = false, mrb, load_string_func;
window.Module = {};
window.Module['print'] = function (x) {
lines.push(x);
printed = true;
};
jQuery.fn.autogrow = function(options) {
var settings = $.extend({
extraLineHeight: 15,
timeoutBuffer: 100
}, options)
var self = this;
var timerId = self.removeData('timerId');
if (timerId)
clearTimeout(timerId);
var handler = function() {
self.each(function (i,e) {
var scrollHeight = e.scrollHeight;
var clientHeight = e.clientHeight;
if (clientHeight < scrollHeight) {
$(e).height(scrollHeight + settings.extraLineHeight);
}
});
self.removeData('timerId');
};
self.data('timerId', setTimeout(handler, settings.timeoutBuffer))
}
$(document).on('keyup.autogrow', 'textarea', function() { $(this).autogrow(); });
$(document).ready(function() {
mrb = Module['_driver_open']();
load_string_func = Module.cwrap("driver_execute_string",
"number",
["number", "string"]);
$("pre").on("click.replace", function() {
var $this = $(this);
$this.off("click.replace");
var form = $("<form>");
var btn = $("<button>Eval</button>");
var textarea = $("<textarea style='width:100%'></textarea>").html($this.html());
var output = $("<pre style='background:snow;display:none'></pre>");
form.append(btn).append("<br>").append(textarea).append("<br>").append(output);
textarea.autogrow();
btn.on("click", function(e) {
lines = [];
printed = false;
load_string_func(mrb, textarea.val());
if (!printed) {
window.Module['print']('<small><i>(no output)</i></small>');
}
output.html(lines.join("<br>")).show();
return false;
});
$this.html(form);
});
window.onbeforeunload = function () {
Module['_driver_close'](mrb);
}
});
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment