Module: Ki::KiEnumerable
- Included in:
- Array, Enumerable
- Defined in:
- lib/util/ruby_extensions.rb
Instance Method Summary collapse
- #any_matches?(value) ⇒ Boolean
- #find_first(count = 1, &block) ⇒ Object
- #size!(*args) ⇒ Object
- #to_h(separator = nil, &block) ⇒ Object
Instance Method Details
#any_matches?(value) ⇒ Boolean
36 37 38 39 40 41 42 43 |
# File 'lib/util/ruby_extensions.rb', line 36 def any_matches?(value) each do |pattern| if value.match(pattern) return pattern end end false end |
#find_first(count = 1, &block) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/util/ruby_extensions.rb', line 45 def find_first(count=1, &block) ret = [] each do |item| if block.nil? || block.call(item) ret << item if ret.size == count break end end end if count==1 ret.at(0) else ret end end |
#size!(*args) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/util/ruby_extensions.rb', line 19 def size!(*args) args.each do |valid_size| if valid_size.kind_of?(Range) if valid_size.include?(size) return self end elsif valid_size.respond_to?(:to_i) if Integer(valid_size) == size return self end else raise "'#{valid_size.inspect}' not supported, needs to be either Range or have .to_i method" end end raise "size #{size} does not match '#{args.map { |s| s.to_s }.join("', '")}'" end |
#to_h(separator = nil, &block) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/util/ruby_extensions.rb', line 62 def to_h(separator=nil, &block) ret = {} each do |item| if separator key, *values = item.split(separator) if values.size > 0 || item.include?(separator) ret[key]=values.join(separator) else ret[key]=true end elsif block key, value = block.call(item) ret[key]=value end end ret end |