「Ruby を実行」の版間の差分

提供: MeryWiki
ナビゲーションに移動 検索に移動
Nnnn (トーク | 投稿記録)
ページの作成:「選択範囲をevalして結果をその場に出します。Ruby本体(https://github.com/oneclick/rubyinstaller2/releases)が必要です。電卓代わりにどうぞ…」
 
MSY-07 (トーク | 投稿記録)
SyntaxHighlightにcopyの追加
 
(2人の利用者による、間の8版が非表示)
1行目: 1行目:
選択範囲をevalして結果をその場に出します。Ruby本体(https://github.com/oneclick/rubyinstaller2/releases)が必要です。電卓代わりにどうぞ。
選択範囲をRubyでevalして結果をその場に出します。[https://github.com/oneclick/rubyinstaller2/releases Ruby本体]が必要です。


以下のショートカット用の変数とメソッドを定義しています。
以下のショートカット用の変数とメソッドを定義しています。
5行目: 5行目:
;<code>r(シンボル, シンボル, シンボル...)</code>
;<code>r(シンボル, シンボル, シンボル...)</code>
:引数を<code>require</code>
:引数を<code>require</code>
;<code>e(文字列)</code>
:引数をURIエンコード (文字化け対策。出力には<code>decodeURIComponent</code>をかませています)
;<code>$_</code>
;<code>$_</code>
:eval中のテキスト
:eval中のテキスト
11行目: 13行目:


== 置き換え版 ==
== 置き換え版 ==
<source lang="javascript">
<syntaxhighlight lang="javascript" copy>
// Rubyを実行 (置き換え版).js
// Rubyを実行 (置き換え版).js
'use strict'


// rubyw.exe の場所を指定
// rubyw.exe の場所を指定
var RUBY_PATH = 'C:\\local\\rubyinstaller-2.6.3-1-x64\\bin\\rubyw.exe'
var RUBY_PATH = 'C:\\Program Files\\rubyinstaller-2.7.1-1-x64\\bin\\rubyw.exe';


if (document.selection.IsEmpty) {
if (document.selection.IsEmpty) {
   document.selection.EndOfLine(false, mePosLogical)
   document.selection.EndOfLine(false, mePosLogical);
   document.selection.StartOfLine(true, mePosLogical)
   document.selection.StartOfLine(true, mePosLogical);
}
}


var shell = new ActiveXObject('WScript.Shell')
var shell = new ActiveXObject('WScript.Shell');
shell.CurrentDirectory = document.Path || shell.SpecialFolders("Desktop")
shell.CurrentDirectory = document.Path || shell.SpecialFolders("Desktop");


var exec = shell.Exec(
var exec = shell.Exec(
   RUBY_PATH +
   '"' + RUBY_PATH + '"' +
   ' -ruri/common -rtimeout' +
   ' -ruri/common -rtimeout' +
   ' -e "' +
   ' -e "' +
33行目: 34行目:


     'def self.r(*s) s.each {|x| require x.to_s } end;' +
     'def self.r(*s) s.each {|x| require x.to_s } end;' +
    'def self.e(str, enc=nil) URI.encode_www_form_component(str.to_s, enc).tr(?+, %_ _) end;' +


     '$0 = URI.decode_www_form_component(STDIN.gets.rstrip);' +
     '$0 = URI.decode_www_form_component(STDIN.gets.rstrip);' +
41行目: 43行目:


     'begin;' +
     'begin;' +
       'print Timeout.timeout(2) { URI.encode_www_form_component(eval($_)).tr(?+, %_ _) };' +
       'print Timeout.timeout(2) { e(eval($_)) };' +
     'rescue Exception => e;' +
     'rescue Exception => e;' +
       'print URI.encode_www_form_component(%_#<#{e.full_message}>_).tr(?+, %_ _);' +
       'print e(%_#<#{e.full_message}>_);' +
     'end' +
     'end' +
   '"')
   '"');


exec.StdIn.WriteLine(encodeURIComponent(document.FullName))
exec.StdIn.WriteLine(encodeURIComponent(document.FullName));
exec.StdIn.Write(encodeURIComponent(document.selection.text))
exec.StdIn.Write(encodeURIComponent(document.selection.text));
exec.StdIn.Close()
exec.StdIn.Close();
document.selection.text = decodeURIComponent(exec.StdOut.ReadAll())
document.selection.text = decodeURIComponent(exec.StdOut.ReadAll());
</source>
</syntaxhighlight>


== アウトプットバー版 ==
== アウトプットバー版 ==
<source lang="javascript">
<syntaxhighlight lang="javascript" copy>
// Rubyを実行 (置き換え版).js
// Rubyを実行 (アウトプットバー版).js
'use strict'
'use strict';


// rubyw.exe の場所を指定
// rubyw.exe の場所を指定
var RUBY_PATH = 'C:\\local\\rubyinstaller-2.6.3-1-x64\\bin\\rubyw.exe'
var RUBY_PATH = 'C:\\Program Files\\rubyinstaller-2.7.1-1-x64\\bin\\rubyw.exe';


if (document.selection.IsEmpty) {
if (document.selection.IsEmpty) {
   document.selection.EndOfLine(false, mePosLogical)
   document.selection.EndOfLine(false, mePosLogical);
   document.selection.StartOfLine(true, mePosLogical)
   document.selection.StartOfLine(true, mePosLogical);
}
}


var shell = new ActiveXObject('WScript.Shell')
var shell = new ActiveXObject('WScript.Shell');
shell.CurrentDirectory = document.Path || shell.SpecialFolders("Desktop")
shell.CurrentDirectory = document.Path || shell.SpecialFolders("Desktop");


var exec = shell.Exec(
var exec = shell.Exec(
   RUBY_PATH +
   '"' + RUBY_PATH + '"' +
   ' -ruri/common -rtimeout' +
   ' -ruri/common -rtimeout' +
   ' -e "' +
   ' -e "' +
76行目: 78行目:


     'def self.r(*s) s.each {|x| require x.to_s } end;' +
     'def self.r(*s) s.each {|x| require x.to_s } end;' +
    'def self.e(str, enc=nil) URI.encode_www_form_component(str.to_s, enc).tr(?+, %_ _) end;' +


     '$0 = URI.decode_www_form_component(STDIN.gets.rstrip);' +
     '$0 = URI.decode_www_form_component(STDIN.gets.rstrip);' +
     '$_ = URI.decode_www_form_component(STDIN.read);' +
     '$_ = URI.decode_www_form_component(STDIN.read);' +
     'def $_.rest;' +
     'def $_.rest;' +
       'self =~ /^__END__/ and $\'[1..-1];' +
       'self =~ /^__END__/ and $\'[1..-1];' +
85行目: 87行目:


     'begin;' +
     'begin;' +
       'print Timeout.timeout(2) { URI.encode_www_form_component(eval($_)).tr(?+, %_ _) };' +
       'print Timeout.timeout(2) { e(eval($_)) };' +
     'rescue Exception => e;' +
     'rescue Exception => e;' +
       'print URI.encode_www_form_component(%_#<#{e.full_message}>_).tr(?+, %_ _);' +
       'print e(%_#<#{e.full_message}>_);' +
     'end' +
     'end' +
   '"')
   '"');


exec.StdIn.WriteLine(encodeURIComponent(document.FullName))
exec.StdIn.WriteLine(encodeURIComponent(document.FullName));
exec.StdIn.Write(encodeURIComponent(document.selection.text))
exec.StdIn.Write(encodeURIComponent(document.selection.text));
exec.StdIn.Close()
exec.StdIn.Close();


outputbar.visible = true
outputbar.visible = true;
outputbar.Writeln(decodeURIComponent(exec.StdOut.ReadAll()))
outputbar.Writeln(decodeURIComponent(exec.StdOut.ReadAll()));
</source>
</syntaxhighlight>

2025年6月30日 (月) 00:42時点における最新版

選択範囲をRubyでevalして結果をその場に出します。Ruby本体が必要です。

以下のショートカット用の変数とメソッドを定義しています。

r(シンボル, シンボル, シンボル...)
引数をrequire
e(文字列)
引数をURIエンコード (文字化け対策。出力にはdecodeURIComponentをかませています)
$_
eval中のテキスト
$_.rest
eval中のテキストのうち、__END__以降

置き換え版[編集]

// Rubyを実行 (置き換え版).js

// rubyw.exe の場所を指定
var RUBY_PATH = 'C:\\Program Files\\rubyinstaller-2.7.1-1-x64\\bin\\rubyw.exe';

if (document.selection.IsEmpty) {
  document.selection.EndOfLine(false, mePosLogical);
  document.selection.StartOfLine(true, mePosLogical);
}

var shell = new ActiveXObject('WScript.Shell');
shell.CurrentDirectory = document.Path || shell.SpecialFolders("Desktop");

var exec = shell.Exec(
  '"' + RUBY_PATH + '"' +
  ' -ruri/common -rtimeout' +
  ' -e "' +
    'STDERR.reopen STDOUT;' +

    'def self.r(*s) s.each {|x| require x.to_s } end;' +
    'def self.e(str, enc=nil) URI.encode_www_form_component(str.to_s, enc).tr(?+, %_ _) end;' +

    '$0 = URI.decode_www_form_component(STDIN.gets.rstrip);' +
    '$_ = URI.decode_www_form_component(STDIN.read);' +
    'def $_.rest;' +
      'self =~ /^__END__/ and $\'[1..-1];' +
    'end;' +

    'begin;' +
      'print Timeout.timeout(2) { e(eval($_)) };' +
    'rescue Exception => e;' +
      'print e(%_#<#{e.full_message}>_);' +
    'end' +
  '"');

exec.StdIn.WriteLine(encodeURIComponent(document.FullName));
exec.StdIn.Write(encodeURIComponent(document.selection.text));
exec.StdIn.Close();
document.selection.text = decodeURIComponent(exec.StdOut.ReadAll());

アウトプットバー版[編集]

// Rubyを実行 (アウトプットバー版).js
'use strict';

// rubyw.exe の場所を指定
var RUBY_PATH = 'C:\\Program Files\\rubyinstaller-2.7.1-1-x64\\bin\\rubyw.exe';

if (document.selection.IsEmpty) {
  document.selection.EndOfLine(false, mePosLogical);
  document.selection.StartOfLine(true, mePosLogical);
}

var shell = new ActiveXObject('WScript.Shell');
shell.CurrentDirectory = document.Path || shell.SpecialFolders("Desktop");

var exec = shell.Exec(
  '"' + RUBY_PATH + '"' +
  ' -ruri/common -rtimeout' +
  ' -e "' +
    'STDERR.reopen STDOUT;' +

    'def self.r(*s) s.each {|x| require x.to_s } end;' +
    'def self.e(str, enc=nil) URI.encode_www_form_component(str.to_s, enc).tr(?+, %_ _) end;' +

    '$0 = URI.decode_www_form_component(STDIN.gets.rstrip);' +
    '$_ = URI.decode_www_form_component(STDIN.read);' +
    'def $_.rest;' +
      'self =~ /^__END__/ and $\'[1..-1];' +
    'end;' +

    'begin;' +
      'print Timeout.timeout(2) { e(eval($_)) };' +
    'rescue Exception => e;' +
      'print e(%_#<#{e.full_message}>_);' +
    'end' +
  '"');

exec.StdIn.WriteLine(encodeURIComponent(document.FullName));
exec.StdIn.Write(encodeURIComponent(document.selection.text));
exec.StdIn.Close();

outputbar.visible = true;
outputbar.Writeln(decodeURIComponent(exec.StdOut.ReadAll()));
スポンサーリンク