Class: Word
- Inherits:
-
Object
- Object
- Word
- Defined in:
- lib/structures/word.rb
Overview
The core structure that stores information of the string to owoify and performs actual owoification.
Instance Method Summary collapse
-
#initialize(word) ⇒ Word
constructor
A new instance of Word.
- #replace(search_value, replace_value, replace_replaced_words = false) ⇒ Object
- #replace_with_func_multiple(search_value, func, replace_replaced_words = false) ⇒ Object
- #replace_with_func_single(search_value, func, replace_replaced_words = false) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(word) ⇒ Word
Returns a new instance of Word.
7 8 9 10 |
# File 'lib/structures/word.rb', line 7 def initialize(word) @replaced_words = Set.new @word = word end |
Instance Method Details
#replace(search_value, replace_value, replace_replaced_words = false) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/structures/word.rb', line 30 def replace(search_value, replace_value, replace_replaced_words = false) return self if !replace_replaced_words && search_value_contains_replaced_words(search_value, replace_value) replacing_word = @word replacing_word = @word.gsub(search_value, replace_value) if search_value.match? @word collection = @word.scan(search_value).flatten replaced_words = collection.length > 1 ? collection.map { |x| x.gsub(x, replace_value) } : [] if replacing_word != @word replaced_words.each do |word| @replaced_words.add(word) end @word = replacing_word end self end |
#replace_with_func_multiple(search_value, func, replace_replaced_words = false) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/structures/word.rb', line 68 def replace_with_func_multiple(search_value, func, replace_replaced_words = false) return self unless search_value.match? @word word = @word captures = search_value.match(word) replace_value = func.call(captures[1], captures[2]) return self if !replace_replaced_words && search_value_contains_replaced_words(search_value, replace_value) replacing_word = @word.gsub(captures[0], replace_value) collection = @word.scan(search_value).flatten replaced_words = collection.length > 1 ? collection.map { |x| x.gsub(x, replace_value) } : [] if replacing_word != @word replaced_words.each do |w| @replaced_words.add(w) end @word = replacing_word end self end |
#replace_with_func_single(search_value, func, replace_replaced_words = false) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/structures/word.rb', line 47 def replace_with_func_single(search_value, func, replace_replaced_words = false) replace_value = func.call return self if !replace_replaced_words && search_value_contains_replaced_words(search_value, replace_value) replacing_word = @word if search_value.match? @word match = @word.match(search_value)[0] replacing_word = @word.gsub(match, replace_value) end collection = @word.scan(search_value).flatten replaced_words = collection.length > 1 ? collection.map { |x| x.gsub(x, replace_value) } : [] if replacing_word != @word replaced_words.each do |word| @replaced_words.add(word) end @word = replacing_word end self end |
#to_s ⇒ Object
12 13 14 |
# File 'lib/structures/word.rb', line 12 def to_s @word end |