Module: Enumerable

Defined in:
lib/rook/helper/enumerable.rb

Instance Method Summary collapse

Instance Method Details

#add_prefix(prefix) ⇒ Object

add prefix for each item

ex.

['a', 'b', 'c'].add_prefix('dir/') #=> ["dir/a", "dir/b", "dir/c"]


28
29
30
# File 'lib/rook/helper/enumerable.rb', line 28

def add_prefix(prefix)
  return self.collect { |item| "#{prefix}#{item}" }
end

#add_suffix(suffix) ⇒ Object

add suffix for each item

ex.

['a', 'b', 'c'].add_suffix('.txt') #=> ["a.txt", "b.txt", "c.txt"]


39
40
41
# File 'lib/rook/helper/enumerable.rb', line 39

def add_suffix(suffix)
  return self.collect { |item| "#{item}#{suffix}" }
end

#apply(method_name, *args) ⇒ Object

apply method with args

ex.

[1, 2, 3].apply(:'+', 5)  #=> [6, 7, 8]


17
18
19
# File 'lib/rook/helper/enumerable.rb', line 17

def apply(method_name, *args)
  return self.collect { |item| item.__send__(method_name, *args) }
end

#basenamesObject

get basename of each item

ex.

['dir1/a.txt', 'b.txt'].basenames() #=> ['a.txt', 'b.txt']


105
106
107
# File 'lib/rook/helper/enumerable.rb', line 105

def basenames
  return self.collect { |item| File.basename(item) }
end

#delete_suffix(suffix = /\.\w+\z/) ⇒ Object

delete suffix for each item

ex.

['a.txt', 'b.txt'].delete_suffix() #=> ["a", "b"]


91
92
93
94
95
96
# File 'lib/rook/helper/enumerable.rb', line 91

def delete_suffix(suffix=/\.\w+\z/)
  unless suffix.is_a?(Regexp)
    suffix = Regexp.compile("#{suffix.to_s}\\z")
  end
  return self.collect { |item| item.sub(suffix, '') }
end

#dirnamesObject

get dirname of each item

ex.

['dir1/a.txt', 'b.txt'].dirnames() #=> ['dir1', '.']


116
117
118
# File 'lib/rook/helper/enumerable.rb', line 116

def dirnames
  return self.collect { |item| File.dirname(item) }
end

#each_gsub(pattern, replace, &block) ⇒ Object

apply ‘gsub()’ method to each item

ex.

['a.txt', 'b.txt'].each_gsub(/\.txt$/, '.html') #=> ["a.html", "b.html"]


76
77
78
79
80
81
82
# File 'lib/rook/helper/enumerable.rb', line 76

def each_gsub(pattern, replace, &block)
  if replace
    return self.collect { |item| item.gsub(pattern, replace) }
  else
    return self.collect { |item| item.gsub(pattern, &block) }
  end
end

#each_sub(pattern, replace, &block) ⇒ Object

apply ‘sub()’ method to each item

ex.

['a.txt', 'b.txt'].each_sub(/\.txt$/, '.html') #=> ["a.html", "b.html"]


61
62
63
64
65
66
67
# File 'lib/rook/helper/enumerable.rb', line 61

def each_sub(pattern, replace, &block)
  if replace
    return self.collect { |item| item.sub(pattern, replace) }
  else
    return self.collect { |item| item.sub(pattern, &block) }
  end
end

#sandwich(prefix, suffix) ⇒ Object

add prefix and suffix for each item

ex.

['a','b','c'].sandwich('p/', '.s') #=> ["p/a.s", "p/b.s", "p/c.s"]


50
51
52
# File 'lib/rook/helper/enumerable.rb', line 50

def sandwich(prefix, suffix)
  return self.collect { |item| "#{prefix}#{item}#{suffix}" }
end