Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/hrules.rb

Instance Method Summary collapse

Instance Method Details

#append_character(character) ⇒ Object



69
70
71
# File 'lib/hrules.rb', line 69

def append_character(character)
  "#{self}#{character}"
end

#delete_at_position(position) ⇒ Object



85
86
87
# File 'lib/hrules.rb', line 85

def delete_at_position(position)
  self.split("").reject.with_index { |v, i| i == position.to_i }.join("")
end

#delete_firstObject



77
78
79
# File 'lib/hrules.rb', line 77

def delete_first
  self[1..-1]
end

#delete_lastObject



81
82
83
# File 'lib/hrules.rb', line 81

def delete_last
  self[0..-2]
end

#dup_block_back(position) ⇒ Object



189
190
191
192
193
# File 'lib/hrules.rb', line 189

def dup_block_back(position)
  block = self[position.to_i..-1]
  self.insert(-1, block) if block
  self
end

#dup_block_forward(position) ⇒ Object



195
196
197
198
199
# File 'lib/hrules.rb', line 195

def dup_block_forward(position)
  block = self[0..position.to_i]
  self.insert(0, block) if block
  self
end

#dup_first_n_times(times) ⇒ Object



135
136
137
138
139
140
# File 'lib/hrules.rb', line 135

def dup_first_n_times(times)
  self[0] = self[0] * times.to_i
  return self
rescue IndexError
  self
end

#dup_last_n_times(times) ⇒ Object



142
143
144
145
146
147
# File 'lib/hrules.rb', line 142

def dup_last_n_times(times)
  self[-1] = (self[-1] + (self[-1] * times.to_i))
  return self
rescue IndexError
  self
end

#duplicateObject



40
41
42
# File 'lib/hrules.rb', line 40

def duplicate
  return "#{self}#{self}"
end

#duplicate_allObject



149
150
151
152
153
154
155
156
157
# File 'lib/hrules.rb', line 149

def duplicate_all
  returned = ""
  self.split("").each.with_index do |v, i|
    returned << self[i] = v * 2
  end
  returned
rescue IndexError
  self
end

#duplicate_n_times(times) ⇒ Object



53
54
55
# File 'lib/hrules.rb', line 53

def duplicate_n_times(times)
  return self * times.to_i
end

#extract_from_position(positions) ⇒ Object



89
90
91
92
# File 'lib/hrules.rb', line 89

def extract_from_position(positions)
  start_p, end_p = positions[0].to_i, positions[1].to_i
  self[start_p..end_p]
end

#insert_at_x(positions) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/hrules.rb', line 100

def insert_at_x(positions)
  loc, char = positions[0].to_i, positions[1]
  if loc.between?(0, (self.length - 1))
    self.insert(loc, char)
  end
  self
end

#inverted_capitalizeObject



44
45
46
# File 'lib/hrules.rb', line 44

def inverted_capitalize
  self.capitalize.swapcase
end

#mutate(rule_string) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/hrules.rb', line 159

def mutate(rule_string)
  to_return = self
  skip      = 0
  rule_string.each_char.with_index do |character, index|
    if index < skip
      next
    end
    rule = Rules.find_rule(character)
    next unless rule
    if rule.takes_parameters?
      thing     = rule_string[ ((index +1 )..(index + rule.param_count)) ]
      to_return = to_return.send(rule.mutation, thing )
      skip      = index + rule.param_count + 1
    else
      to_return = to_return.send(rule.mutation)
    end
  end
  return to_return
end

#omit_from_position(positions) ⇒ Object



94
95
96
97
98
# File 'lib/hrules.rb', line 94

def omit_from_position(positions)
  start_p, end_p = positions[0].to_i, positions[1].to_i
  range = (start_p..end_p).to_a
  self.split("").reject.with_index { |v, i| range.include?(i) }.join("")
end

#overwrite_at_x(positions) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/hrules.rb', line 108

def overwrite_at_x(positions)
  loc, char = positions[0].to_i, positions[1]
  self[loc] = char
  self
rescue IndexError
  self
end

#prepend_character(character) ⇒ Object



73
74
75
# File 'lib/hrules.rb', line 73

def prepend_character(character)
  "#{character}#{self}"
end

#purge_instances_of_x(character) ⇒ Object



129
130
131
132
133
# File 'lib/hrules.rb', line 129

def purge_instances_of_x(character)
  self.gsub(character, "")
rescue IndexError
  self
end

#reflectObject



57
58
59
# File 'lib/hrules.rb', line 57

def reflect
  "#{self}#{self.reverse}"
end

#replace_with_char(characters) ⇒ Object



122
123
124
125
126
127
# File 'lib/hrules.rb', line 122

def replace_with_char(characters)
  replace, replace_with = characters.split("")[0], characters.split("")[1]
  self.gsub(replace, replace_with)
rescue IndexError
  self
end

#rotate_leftObject



61
62
63
# File 'lib/hrules.rb', line 61

def rotate_left
  self.split("").rotate(1).join("")
end

#rotate_rightObject



65
66
67
# File 'lib/hrules.rb', line 65

def rotate_right
  self.split("").rotate(-1).join("")
end

#swap(characters) ⇒ Object



179
180
181
182
183
184
185
186
187
# File 'lib/hrules.rb', line 179

def swap(characters)
  pos1, pos2 = characters.split("")[0].to_i, characters.split("")[1].to_i
  if pos1.between?(0, (self.length - 1)) and pos2.between?(0, (self.length - 1)) 
    self[pos1] = self[pos2]
  end
  self
rescue IndexError
  self
end

#toggle_at_position(position) ⇒ Object



48
49
50
51
# File 'lib/hrules.rb', line 48

def toggle_at_position(position)
  if self[position.to_i] then self[position.to_i] = self[position.to_i]&.upcase end
  return self
end

#truncate_from_x(position) ⇒ Object



116
117
118
119
120
# File 'lib/hrules.rb', line 116

def truncate_from_x(position)
  self[0..position.to_i]
rescue IndexError
  self
end