Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/opensecret/additions/array.rb
Overview
– – Reopen the core ruby Array class and add the below methods to it. – – Case Sensitivity rules for [ALL] the below methods that are – added to the core Ruby string class. – – For case insensitive behaviour make sure you downcase both the – string object and the parameter strings (or strings within – other parameter objects, like arrays and hashes). –
Instance Method Summary collapse
-
#before_and_after(begin_delimeter, end_delimeter) ⇒ Object
– – Get the text [in between] this and that delimeter [exclusively].
- #middlle_bit(begin_delimeter, end_delimeter) ⇒ Object
Instance Method Details
#before_and_after(begin_delimeter, end_delimeter) ⇒ Object
– – Get the text [in between] this and that delimeter [exclusively]. – Exclusively means the returned text [does not] include either of – the matched delimeters (although an unmatched instance of [this] – delimeter may appear in the in-between text). – – ——————– – Multiple Delimiters – ——————– – – When multiple delimiters exist, the text returned is in between the – – [a] - first occurrence of [this] delimeter AND the – [b] - 1st occurrence of [that] delimeter [AFTER] the 1st delimiter – – Instances of [that] delimiter occurring before [this] are ignored. – The text could contain [this] delimeter instances but is guaranteed – not to contain a [that] delimeter. – – ———– – Parameters – ———– – – this_delimiter : begin delimeter (not included in returned string) – that_delimiter : end delimeter (not included in returned string) – – ———– – Exceptions – ———– – – An exception (error) will be thrown if – – => any nil (or empties) exist in the input parameters – => [this] delimeter does not appear in the in_string – => [that] delimeter does not appear after [this] one –
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/opensecret/additions/array.rb', line 51 def before_and_after begin_delimeter, end_delimeter Throw.if_nil_or_empty_strings [ self, begin_delimeter, end_delimeter ] before_after_lines = [] in_middle_bit = false self.each do |candidate_line| is_middle_boundary = !in_middle_bit && candidate_line.downcase.include?(begin_delimeter.downcase) if is_middle_boundary in_middle_bit = true next end unless in_middle_bit before_after_lines.push candidate_line next end #-- #-- Now we are definitely in the middle bit. #-- Let's check for the middle end delimeter #-- if candidate_line.downcase.include? end_delimeter.downcase in_middle_bit = false end end return before_after_lines end |
#middlle_bit(begin_delimeter, end_delimeter) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/opensecret/additions/array.rb', line 86 def middlle_bit begin_delimeter, end_delimeter Throw.if_nil_or_empty_strings [ self, begin_delimeter, end_delimeter ] middle_lines = [] in_middle_bit = false self.each do |candidate_line| is_middle_boundary = !in_middle_bit && candidate_line.downcase.include?(begin_delimeter.downcase) if is_middle_boundary in_middle_bit = true next end end_of_middle = in_middle_bit && candidate_line.downcase.include?(end_delimeter.downcase) return middle_lines if end_of_middle #-- #-- We are definitely in the middle bit. #-- middle_lines.push(candidate_line) if in_middle_bit end unreachable_str = "This point should be unreachable unless facts are ended." raise RuntimeError.new unreachable_str end |