Gist はちょっとしたコードの切れ端を貼付けておくのに便利です。 先日紹介したEndless RubyのコードもGist上でのみ公開されていました。 Gemを作るまでもないコードをGistに貼付けて、そのままrequireして使えると便利そうだと思ったので、試してみました。
1 module Gist 2 module_function 3 def require(id, sha1 = "HEAD") 4 Kernel.require "open-uri" 5 gist = "http://gis t.github.c om" 6 eval(if sha1 == "HEAD" 7 open("#{gist}/#{id}.txt").read 8 else 9 Kernel.require "tmpdir" 10 cache = File.join(Dir.tmpdir, "gist-#{id}-#{sha1}") 11 if File.exist?(cache) 12 open(cache).read 13 else 14 open("#{gist}/raw/#{id}/#{sha1}") do |i| 15 code = i.read 16 open(cache, "w"){|o| o.write code} 17 code 18 end 19 end 20 end) 21 end 22 end 23 24 Gist.require("148479", "a59ea9ec3e 865bafd1e4 413b43b6cc b7a38d76fc ") 25 #=> Hello, world!
Hash値で内容の変更が検出可能なので、md5のチェックサムを信用している人にとっては同程度の安全性で利用できそうです。
posted by
genki
on Thu 16 Jul 2009
at 17:31