Module: Aai::Utils

Included in:
Aai
Defined in:
lib/aai/utils.rb

Instance Method Summary collapse

Instance Method Details

#check_command(cmd) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/aai/utils.rb', line 54

def check_command cmd
  path = command? cmd

  AbortIf.abort_unless path,
                       "Missing #{cmd} command. " +
                       "Is it executable and on your path?"

  path
end

#check_files(fnames) ⇒ Object

Raises SystemExit if one of the fnames does not exist.



8
9
10
11
12
# File 'lib/aai/utils.rb', line 8

def check_files fnames
  fnames.each do |fname|
    AbortIf.abort_unless_file_exists fname
  end
end

#clean_str(str) ⇒ Object



3
4
5
# File 'lib/aai/utils.rb', line 3

def clean_str str
  str.strip.gsub(/[^\p{Alnum}_]+/, "_").gsub(/_+/, "_")
end

#command?(cmd) ⇒ Boolean

from stackoverflow.com/questions/2108727/ \ which-in-ruby-checking-if-program-exists-in-path-from-ruby

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
50
51
52
# File 'lib/aai/utils.rb', line 42

def command? cmd
  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    exts.each do |ext|
      exe = File.join(path, "#{cmd}#{ext}")
      return exe if File.executable?(exe) && !File.directory?(exe)
    end
  end

  return nil
end

#one_way_combinations(a1, a2, no_duplicates = true) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/aai/utils.rb', line 26

def one_way_combinations a1, a2, no_duplicates=true
  permutations = []

  a1.each do |elem1|
    a2.each do |elem2|
      if !no_duplicates || (no_duplicates && elem1 != elem2)
        permutations << [elem1, elem2]
      end
    end
  end

  permutations
end

#two_ary_permutations(a1, a2) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/aai/utils.rb', line 14

def two_ary_permutations a1, a2
  permutations = []

  a1.each do |elem1|
    a2.each do |elem2|
      permutations << [elem1, elem2] << [elem2, elem1]
    end
  end

  permutations
end