Class: Array
- Defined in:
- lib/sugar-high/blank.rb,
lib/sugar-high/file.rb,
lib/sugar-high/array.rb,
lib/sugar-high/regexp.rb,
lib/sugar-high/arguments.rb
Overview
:nodoc:
Instance Method Summary collapse
- #args ⇒ Object
- #extract(sym) ⇒ Object
-
#extract_options! ⇒ Object
Repeat overall-used method here, to use it without Rails.
- #file_join ⇒ Object
- #file_names(ext = '*') ⇒ Object
- #flat_uniq ⇒ Object
- #flat_uniq! ⇒ Object
- #grep_it(expr) ⇒ Object
- #last_arg(default = {}) ⇒ Object
- #last_arg_value(default = nil) ⇒ Object
- #last_option ⇒ Object
- #none? ⇒ Boolean
- #pick(num) ⇒ Object
- #pick_one! ⇒ Object
- #select!(&block) ⇒ Object
- #to_filenames ⇒ Object
- #to_filenames! ⇒ Object
- #to_files ⇒ Object
- #to_files! ⇒ Object
- #to_paths ⇒ Object
- #to_paths! ⇒ Object
- #to_strings ⇒ Object
- #to_strings! ⇒ Object
- #to_symbols ⇒ Object
- #to_symbols! ⇒ Object
- #to_symbols_num ⇒ Object
- #to_symbols_num! ⇒ Object
- #to_symbols_uniq ⇒ Object
- #to_symbols_uniq! ⇒ Object
- #without(*values) ⇒ Object
Instance Method Details
#args ⇒ Object
2 3 4 |
# File 'lib/sugar-high/arguments.rb', line 2 def args flatten.map{|a| a.args}.flatten end |
#extract(sym) ⇒ Object
106 107 108 |
# File 'lib/sugar-high/array.rb', line 106 def extract(sym) map { |e| e.send(sym) } end |
#extract_options! ⇒ Object
Repeat overall-used method here, to use it without Rails
111 112 113 |
# File 'lib/sugar-high/array.rb', line 111 def last.is_a?(::Hash) ? pop : {} end |
#file_join ⇒ Object
77 78 79 |
# File 'lib/sugar-high/array.rb', line 77 def file_join File.join(*self.flatten) end |
#file_names(ext = '*') ⇒ Object
34 35 36 |
# File 'lib/sugar-high/file.rb', line 34 def file_names ext = '*' self.map{|a| a.gsub( /(.*)\//, '').gsub(/\.#{Regexp.escape(ext.to_s)}/, '')} end |
#flat_uniq ⇒ Object
95 96 97 |
# File 'lib/sugar-high/array.rb', line 95 def flat_uniq self.flatten.compact.uniq end |
#flat_uniq! ⇒ Object
99 100 101 102 103 104 |
# File 'lib/sugar-high/array.rb', line 99 def flat_uniq! self.flatten! self.compact! self.uniq! self end |
#grep_it(expr) ⇒ Object
24 25 26 27 |
# File 'lib/sugar-high/regexp.rb', line 24 def grep_it expr return self if !expr self.grep(expr.to_regexp) end |
#last_arg(default = {}) ⇒ Object
12 13 14 15 |
# File 'lib/sugar-high/arguments.rb', line 12 def last_arg default = {} last = self.flatten.last last.kind_of?(Hash) ? last : default end |
#last_arg_value(default = nil) ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/sugar-high/arguments.rb', line 17 def last_arg_value default = nil last = self.flatten.last raise ArgumentError, "Default value must be a Hash, was #{default}" if !default.kind_of? Hash key = default.keys.first return default[key] if !last.kind_of? Hash last[key] ? last[key] : default[key] end |
#last_option ⇒ Object
6 7 8 9 10 |
# File 'lib/sugar-high/arguments.rb', line 6 def last_option default = self.last_arg last = self.flatten.last last.kind_of?(Hash) ? last : default end |
#none? ⇒ Boolean
91 92 93 |
# File 'lib/sugar-high/array.rb', line 91 def none? self.flatten.compact.empty? end |
#pick(num) ⇒ Object
11 12 13 |
# File 'lib/sugar-high/array.rb', line 11 def pick num num.times.inject([]) {|res, x| res << pick_one!} end |
#pick_one! ⇒ Object
6 7 8 9 |
# File 'lib/sugar-high/array.rb', line 6 def pick_one! index = Kernel.rand(self.size) self[index] end |
#select!(&block) ⇒ Object
115 116 117 |
# File 'lib/sugar-high/array.rb', line 115 def select! &block replace select &block end |
#to_filenames ⇒ Object
60 61 62 |
# File 'lib/sugar-high/array.rb', line 60 def to_filenames self.to_strings.map(&:underscore) end |
#to_filenames! ⇒ Object
64 65 66 |
# File 'lib/sugar-high/array.rb', line 64 def to_filenames! self.to_strings!.map!(&:underscore) end |
#to_files ⇒ Object
81 82 83 84 |
# File 'lib/sugar-high/array.rb', line 81 def to_files self.map{|fp| fp.path.to_file } self.extend FilesArray end |
#to_files! ⇒ Object
86 87 88 89 |
# File 'lib/sugar-high/array.rb', line 86 def to_files! self.map!{|fp| fp.path.to_file } self.extend FilesArray end |
#to_paths ⇒ Object
68 69 70 |
# File 'lib/sugar-high/array.rb', line 68 def to_paths self.map(&:path) end |
#to_paths! ⇒ Object
72 73 74 75 |
# File 'lib/sugar-high/array.rb', line 72 def to_paths! self.map!(&:path) self end |
#to_strings ⇒ Object
56 57 58 |
# File 'lib/sugar-high/array.rb', line 56 def to_strings self.flatten.select_labels.map(&:to_s) end |
#to_strings! ⇒ Object
51 52 53 54 |
# File 'lib/sugar-high/array.rb', line 51 def to_strings! self.flatten! self.select_labels!.map!(&:to_s) end |
#to_symbols ⇒ Object
21 22 23 24 25 |
# File 'lib/sugar-high/array.rb', line 21 def to_symbols res = self.flatten.select_labels res = res.map{|a| a.to_s.to_sym } if res res || [] end |
#to_symbols! ⇒ Object
27 28 29 30 31 32 |
# File 'lib/sugar-high/array.rb', line 27 def to_symbols! self.flatten! self.select_labels! self.map!{|a| a.to_s.to_sym } self end |
#to_symbols_num ⇒ Object
34 35 36 |
# File 'lib/sugar-high/array.rb', line 34 def to_symbols_num self.flatten.map{|a| a.kind_of?(Fixnum) ? "_#{a}" : a}.map{|a| a.to_s.to_sym } end |
#to_symbols_num! ⇒ Object
38 39 40 41 |
# File 'lib/sugar-high/array.rb', line 38 def to_symbols_num! self.flatten! self.map!{|a| a.kind_of?(Fixnum) ? "_#{a}" : a}.map!{|a| a.to_s.to_sym } end |
#to_symbols_uniq ⇒ Object
43 44 45 |
# File 'lib/sugar-high/array.rb', line 43 def to_symbols_uniq to_symbols.uniq end |
#to_symbols_uniq! ⇒ Object
47 48 49 |
# File 'lib/sugar-high/array.rb', line 47 def to_symbols_uniq! self.to_symbols!.uniq! end |
#without(*values) ⇒ Object
15 16 17 18 19 |
# File 'lib/sugar-high/array.rb', line 15 def without(*values) copy = self.dup values.flatten.each { |value| copy.delete(value) } copy end |