2
2

More than 5 years have passed since last update.

Rubyで``による外部コマンド呼び出しを差し替える

Last updated at Posted at 2014-12-02

レガシーコードのテストに有用です。。。

legacy.rb
def file_list
  `ls /path/to/some_dir`.split
end
test.rb
require './legacy'

orig_method = lambda(&method(:`))
Kernel.module_eval do
  define_method(:`) {|*| %w(local lib include).join("\n")}
end

puts file_list #=> ["local", "lib", "include"]

# 元に戻す
Kernel.module_eval do
  define_method(:`, orig_method)
end
2
2
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
2
2