Class: Array

Inherits:
Object show all
Defined in:
lib/corelib_ruby/core_ext/array/core.rb,
lib/corelib_ruby/core_ext/array/delete.rb,
lib/corelib_ruby/core_ext/array/iterators.rb

Instance Method Summary collapse

Instance Method Details

#cl_delete!(obj) ⇒ Object

Deletes items from the array, but raises an error if the item does not exist.

obj: the obj to delete

Raises:

  • (ArgumentError)


5
6
7
8
9
10
11
12
# File 'lib/corelib_ruby/core_ext/array/delete.rb', line 5

def cl_delete!(obj)
  # do this check first only if the obj is nil because the
  # cost of include? rises linerarly with the size of the array
  raise ArgumentError if obj.nil? && !include?(nil)
  result = self.delete(obj)
  raise ArgumentError.new("#{obj.inspect} not present in array") if result.nil? && !obj.nil?
  result
end

#cl_each_with_last_flagObject

Tterates over the Array just as you would with Array#each. However, for each iteration pass in an extra variable that will be ‘false` until the last iteration.



4
5
6
7
# File 'lib/corelib_ruby/core_ext/array/iterators.rb', line 4

def cl_each_with_last_flag
  my_size = size - 1
  each_with_index {|item, index| yield(item, index == my_size)}
end

#cl_extract_options!Object

Many methods take an optional hash argument to hold options for the method. This method pops that hash off he arguments list if it exists or returns an empty hash.



4
5
6
# File 'lib/corelib_ruby/core_ext/array/core.rb', line 4

def cl_extract_options!
  last.is_a?(Hash) ? pop : {}
end

#cl_not_empty?Boolean

A simple helper to improve readability. [].cl_not_empty? reads better than ![].empty?

Returns:

  • (Boolean)


10
11
12
# File 'lib/corelib_ruby/core_ext/array/core.rb', line 10

def cl_not_empty?
  !empty?
end

#cl_to_yes_no(options = {}) ⇒ Object



14
15
16
# File 'lib/corelib_ruby/core_ext/array/core.rb', line 14

def cl_to_yes_no(options={})
  collect {|e| e.cl_to_yes_no(options)}
end