Module: PlainText::Util
Overview
Contains some utility methods for use in this module and classes.
Class Method Summary collapse
-
.even_odd_arrays(ary, size_even: false, filler: "") ⇒ Object
Returns a pair of Arrays of even and odd number-indices of the original Array.
-
.positive_array_index(i, ary) ⇒ Integer, NilClass
Returns a non-negative Array index for self.
-
.positive_array_index_checked(index_in, ary, accept_too_big: true, varname: nil) ⇒ Integer
Returns a non-negative Array index for self, performing a check.
-
.raise_typeerror(var, to_class, verbose: $DEBUG) ⇒ Object
Raise TypeError.
Class Method Details
.even_odd_arrays(ary, size_even: false, filler: "") ⇒ Object
Returns a pair of Arrays of even and odd number-indices of the original Array
23 24 25 26 27 28 29 30 31 |
# File 'lib/plain_text/util.rb', line 23 def even_odd_arrays(ary, size_even: false, filler: "") ar_even = select.with_index { |_, i| i.even? } rescue select.each_with_index { |_, i| i.even? } # Rescue for Ruby 2.1 or earlier ar_odd = select.with_index { |_, i| i.odd? } rescue select.each_with_index { |_, i| i.odd? } # Rescue for Ruby 2.1 or earlier if size_even && (ar_even.size != ar_odd.size) ar_odd.push filler raise "Should not happern." if (ar_even.size != ar_odd.size) end [ar_even, ar_odd] end |
.positive_array_index(i, ary) ⇒ Integer, NilClass
Returns a non-negative Array index for self
If positive or zero, it returns i. If the negative index is out of range, it returns nil.
43 44 45 46 47 48 |
# File 'lib/plain_text/util.rb', line 43 def positive_array_index(i, ary) i2 = i.to_int rescue (raise TypeError, sprintf("no implicit conversion of #{i.class} into Integer")) return i2 if i2 >= 0 ret = ary.size + i2 rescue (raise ArgumentError, "argument is not an array.") (ret < 0) ? nil : ret end |
.positive_array_index_checked(index_in, ary, accept_too_big: true, varname: nil) ⇒ Integer
Returns a non-negative Array index for self, performing a check.
Exception is raised if it is out of range.
Wrapper for #positive_array_index
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/plain_text/util.rb', line 63 def positive_array_index_checked(index_in, ary, accept_too_big: true, varname: nil) # def self.positive_valid_index_for_array(index_in, ary, varname: nil) errmsgs = {} %w(of for).each do |i| errmsgs[i] = (varname ? "." : sprintf(" %s %s.", i, varname)) end index = positive_array_index(index_in, ary) # guaranteed to be Integer or nil raise IndexError, sprintf("index (%s) too small for array; minimum: -%d", index_in, ary.size) if !index # Ruby default Error message (except the variable "index" as opposed to "index_in is used in the true Ruby default). if index_in >= 0 last_index = ary.size - 1 errnote1 = nil if (index > last_index + 1) && !accept_too_big errnote1 = ' (or +1)' elsif (index == last_index + 1) && (false == accept_too_big) errnote1 = " " end raise IndexError, sprintf("Specified index (%s) is larger than the last index (%d)%s%s", index_in, last_index, errnote1, errmsgs['of']) if errnote1 end index end |
.raise_typeerror(var, to_class, verbose: $DEBUG) ⇒ Object
Raise TypeError
Call as raise_typeerror(var_name) from instance methods, providing this Module is included in the Class/Module.
94 95 96 97 98 |
# File 'lib/plain_text/util.rb', line 94 def raise_typeerror(var, to_class, verbose: $DEBUG) msg1 = (verbose ? sprintf("(<= %s)", var.inspect) : "") to_class_str = (to_class.name rescue to_class.to_str) raise TypeError, sprintf("no implicit conversion of %s%s into %s", var.class, msg1, to_class_str) end |