Module: Utils
- Defined in:
- lib/kimoby/utils.rb
Overview
a set of utility functions
Class Method Summary collapse
- .get_words_with_different_size(arr, size_one, size_two) ⇒ Object
- .get_words_with_same_size(arr, size) ⇒ Object
- .read_from_file(path) ⇒ Object
Class Method Details
.get_words_with_different_size(arr, size_one, size_two) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/kimoby/utils.rb', line 34 def self.get_words_with_different_size(arr, size_one, size_two) starting = Process.clock_gettime(Process::CLOCK_MONOTONIC) res = [] begin if size_one.is_a?(Integer) && size_two.is_a?(Integer) && size_one > 0 && size_two > 0 arr.each do |word| res << word.strip if word.strip.length == size_one || word.strip.length == size_two end end rescue NoMethodError => e raise e end ending = Process.clock_gettime(Process::CLOCK_MONOTONIC) elapsed = ending - starting puts "generate words with different sizes | execution runtime: #{elapsed}" res # return array of words with different sizes end |
.get_words_with_same_size(arr, size) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/kimoby/utils.rb', line 15 def self.get_words_with_same_size(arr, size) starting = Process.clock_gettime(Process::CLOCK_MONOTONIC) res = [] begin if size.is_a?(Integer) && size > 0 arr.each do |word| res << word.strip if word.strip.length == size end end rescue NoMethodError => e raise e end ending = Process.clock_gettime(Process::CLOCK_MONOTONIC) elapsed = ending - starting puts "generate words with same size | execution runtime: #{elapsed}" res # return array of words with same size end |
.read_from_file(path) ⇒ Object
8 9 10 11 12 13 |
# File 'lib/kimoby/utils.rb', line 8 def self.read_from_file(path) data = File.readlines(path).reject { |s| s.strip.empty? }.join data.split("\n").map(&:downcase) rescue Errno::ENOENT => e raise e end |