Top Level Namespace

Defined Under Namespace

Modules: Enumerable, FromHash Classes: Fixnum, Hash, Numeric, Object, Regexp, String, Time

Instance Method Summary collapse

Instance Method Details

#all_dirs_recursive(dir) ⇒ Object



1
2
3
4
5
6
7
8
9
10
# File 'lib/mharris_ext/fileutils.rb', line 1

def all_dirs_recursive(dir)
  raise "null dir" unless dir
  init_slash = (dir[0..0] == '/')
  #raise "can't handle initial slash" if dir[0..0] == '/'
  #puts "all_dirs_recursive #{dir}"
  dir.split("/")[0..-1].inject([]) do |paths,dir|
    last_path = (paths.empty? ? "" : "#{paths[-1]}/")
    paths + ["#{last_path}#{dir}"]
  end.select { |x| x != '' }
end

#eat_exceptionsObject



49
50
51
52
# File 'lib/mharris_ext/fileutils.rb', line 49

def eat_exceptions
  yield
rescue
end

#ec(cmd) ⇒ Object



1
2
3
4
5
6
# File 'lib/mharris_ext/cmd.rb', line 1

def ec(cmd)
  puts cmd
  res = `#{cmd}`
  puts res
  res
end

#mkdir_if(dir) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/mharris_ext/fileutils.rb', line 12

def mkdir_if(dir)
  if FileTest.exists?(dir)
    #puts "not making #{dir}"
  else
    #puts "making #{dir}"
    FileUtils.mkdir(dir) 
  end
end

#mkdir_recursive(ops) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/mharris_ext/fileutils.rb', line 21

def mkdir_recursive(ops)
  dir = ops.is_a?(Hash) ? ops[:dir] : ops
  #puts "mkdir_recursive #{dir}"
  all_dirs_recursive(dir).each do |dir|
    mkdir_if(dir)
  end
end

#mv_making_dir(f, new_dir) ⇒ Object



29
30
31
32
33
# File 'lib/mharris_ext/fileutils.rb', line 29

def mv_making_dir(f,new_dir)
  #puts "mv_making_dir #{f} #{new_dir}"
  mkdir_recursive(new_dir)
  FileUtils.mv(f,new_dir)
end


9
10
11
12
13
14
15
16
17
# File 'lib/mharris_ext/benchmark.rb', line 9

def print_memory_usage!
  Thread.new do
    loop do
      mem = `ps -l #{Process.pid}`.to_a[1].split[8]
      puts "Memory: #{mem} #{Time.now}"
      sleep(10)
    end
  end
end

#rm_r_if(dir) ⇒ Object



45
46
47
# File 'lib/mharris_ext/fileutils.rb', line 45

def rm_r_if(dir)
  FileUtils.rm_r(dir) if FileTest.exists?(dir)
end

#tm(msg = "Thing") ⇒ Object



1
2
3
4
5
6
7
# File 'lib/mharris_ext/benchmark.rb', line 1

def tm(msg="Thing")
  t = Time.now
  res = yield
  seconds = Time.now - t
  puts "#{msg} took #{seconds} seconds"
  res
end