1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

SketchUp の Ruby コンソールで変更のあったファイルだけリロードしてくれるようなモノ

Last updated at Posted at 2018-01-31

Gist にだけあったものをサルベージして Qiita にも。

自分は SketchUp の Ruby コンソールで使うんだけど、開発中にいろいろスクリプトをいじったとして、Ruby コンソールで Matome.clap とやると更新のあったファイルだけリロードしてくれるようなもの

clap-loader.rb
module Matome
  @@clapLoader = nil

  def self.clap(dir=nil)
    if @@clapLoader then
      @@clapLoader.reload
    else
      @@clapLoader = ClapLoader.new dir
    end
  end

  class ClapLoader
    def initialize(dir)
      @own = __FILE__
      @dir = dir.split('/')[0..-2].join('/')
      @mtimes = {}
      reload
    end

    def reload
      $LOADED_FEATURES.each{|f|
        if f != @own && f.start_with?(@dir) then
          File::Stat.new(f).mtime.tap { |mtimeOnFS|
            @mtimes[f].tap { |mtimeOnCache|
              if ! mtimeOnCache then
                @mtimes[f] = mtimeOnFS
              elsif mtimeOnFS > mtimeOnCache then
                warn("LOADING: %s" % f)
                load f
                @mtimes[f] = File::Stat.new(f).mtime
              end
              }
          } rescue warn('skip ... %s' % f)
        end
      }
    end
  end
end

__END__
main.rb
require 'matome/clap-loader'
require 'matome/something-great'

Matome.clap __FILE__
Matome.SomethingGreat

Matome.clap

Qiita と Gist の連携は、先に Gist に書いちゃだめで、Qiita の記事を投稿したときにコード部分が Gist に載るようにするしかないのね。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?