Class: Array
- 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
-
#cl_delete!(obj) ⇒ Object
Deletes items from the array, but raises an error if the item does not exist.
-
#cl_each_with_last_flag ⇒ Object
Tterates over the Array just as you would with Array#each.
-
#cl_extract_options! ⇒ Object
Many methods take an optional hash argument to hold options for the method.
-
#cl_not_empty? ⇒ Boolean
A simple helper to improve readability.
- #cl_to_yes_no(options = {}) ⇒ Object
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
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_flag ⇒ Object
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 last.is_a?(Hash) ? pop : {} end |
#cl_not_empty? ⇒ Boolean
A simple helper to improve readability. [].cl_not_empty? reads better than ![].empty?
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(={}) collect {|e| e.cl_to_yes_no()} end |