• 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

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://gist.github.com"
   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", "a59ea9ec3e865bafd1e4413b43b6ccb7a38d76fc")
  25  #=> Hello, world!

Hash値で内容の変更が検出可能なので、md5のチェックサムを信用している人にとっては同程度の安全性で利用できそうです。

posted by Png genki on Thu 16 Jul 2009 at 17:31

Comments:

or Preview
Social Bookmarks
  • Delicious
  • B_entry1551
  • Clip_16_12_w
Services from s21g
twpro(ツイプロ)
Twitterプロフィールを快適検索
地価2009
土地の値段を調べてみよう
MyRestaurant
自分だけのレストラン手帳
Formula
ブログに数式を埋め込める数式コミュニティ