<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/xml.xsl" type="text/xsl"?><feed xmlns="http://www.w3.org/2005/Atom">
  <title>nulog, NULL::something : out of the headphone &gt; 2005 &gt; January &gt; 22</title>
  <link href="http://lowreal.net/logs/2005/01/22"/>
  <icon>http://lowreal.net/img/banner.png</icon>
  <link rel="self" type="application/atom+xml" href="http://lowreal.net/logs/2005/01/22.atom"/>
  <link rel="alternate" type="application/xhtml+xml" href="http://lowreal.net/logs/2005/01/22.xhtml"/>
  <updated>2005-01-22T12:13:19+09:00</updated>
  <author>
    <name>cho45(砂糖)</name>
  </author>
  <id>http://lowreal.net/2005/01/22</id>
  <entry>
    <title>Ruby コードを安全に実行する。</title>
    <link rel="alternate" type="text/html" href="http://lowreal.net/logs/2005/01/22/1.html"/>
    <link rel="alternate" type="application/xml+xhtml" href="http://lowreal.net/logs/2005/01/22/1.xhtml"/>
    <updated>2005-01-22T03:05:02+09:00</updated>
    <published>2005-01-22T03:05:02+09:00</published>
    <id>http://lowreal.net/2005/01/22/1</id>
    <category term="ruby"/>
    <category term="prog"/>
    <content type="xhtml" xml:base="http://lowreal.net/">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>前にも書いたけど完全じゃないので。たぶん、これでいいはず。</p>
        <pre class="Ruby" title="全く信頼できない Ruby コードを安全に実行">def safe(lvl, tm=1)
    result = nil
    tg = nil
    th = Thread.start(lvl) do |level|
        tg = ThreadGroup.new.add(Thread.current)
        $SAFE = level
        result = yield
    end.join(tm)
    tg.list.each {|t| t.kill}
    raise TimeoutError unless th
    result
end
</pre>
        <p>ThreadGroup を新しく作り、それにカレント実行スレッドを突っ込んで、信頼できないコードから生成されるスレッドを全てこいつの所属させ、実行終了したら kill! kill! kill!</p>
        <p><abbr title="Internet Relay Chat">IRC</abbr> BOT の Ruby コード実行機能をこれにした。まぁしかし！ SEGV されたら終りというどうしようもない脆弱性がありますけれども。</p>
      </div>
    </content>
  </entry>
  <entry>
    <title>Ruby, ブロック引数をとるメソッド</title>
    <link rel="alternate" type="text/html" href="http://lowreal.net/logs/2005/01/22/2.html"/>
    <link rel="alternate" type="application/xml+xhtml" href="http://lowreal.net/logs/2005/01/22/2.xhtml"/>
    <updated>2005-01-22T12:13:19+09:00</updated>
    <published>2005-01-22T12:13:19+09:00</published>
    <id>http://lowreal.net/2005/01/22/2</id>
    <category term="ruby"/>
    <category term="prog"/>
    <content type="xhtml" xml:base="http://lowreal.net/">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <pre class="Ruby">
def foo(a, b)
    yield a + b
end

def fop(a, b, &amp;block)
    yield a + b
end

def foq(a, b, &amp;block)
    block.call a + b
end
</pre>
        <p>引数に <code>&amp;block</code> を書かないとブロックをとるメソッドなのか取らないメソッドなのかわかりにくい。でもなんか <code>&amp;block</code> を書くとダサい。</p>
        <p>二番目の方法だと block という引数を使ってなくてキモい。三番目の方法は一般的なコードじゃなくてキモい。</p>
        <p>さて、ホントは上のコードの block.call は block.yield (引数のチェックをしない) になるんだろうけど、NoMethodError がでる。なんでだろ。</p>
        <pre>$ ruby -v -e 'Proc.new {|t| puts t}.yield("foo")'
ruby 1.8.1 (2003-12-25) [i386-cygwin]
-e:1: undefined method 'yield' for #&lt;Proc:0x100e6dd8@-e:1&gt; (NoMethodError)
exit 1</pre>
      </div>
    </content>
  </entry>
</feed>
