class String

Public Instance Methods

append_character(character) click to toggle source
# File lib/hrules.rb, line 69
def append_character(character)
        "#{self}#{character}"
end
delete_at_position(position) click to toggle source
# 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_first() click to toggle source
# File lib/hrules.rb, line 77
def delete_first
        self[1..-1]
end
delete_last() click to toggle source
# File lib/hrules.rb, line 81
def delete_last
        self[0..-2]
end
dup_block_back(position) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
duplicate() click to toggle source
# File lib/hrules.rb, line 40
def duplicate
        return "#{self}#{self}"
end
duplicate_all() click to toggle source
# 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) click to toggle source
# File lib/hrules.rb, line 53
def duplicate_n_times(times)
        return self * times.to_i
end
extract_from_position(positions) click to toggle source
# 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) click to toggle source
# 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_capitalize() click to toggle source
# File lib/hrules.rb, line 44
def inverted_capitalize
        self.capitalize.swapcase
end
mutate(rule_string) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/hrules.rb, line 73
def prepend_character(character)
        "#{character}#{self}"
end
purge_instances_of_x(character) click to toggle source
# File lib/hrules.rb, line 129
def purge_instances_of_x(character)
        self.gsub(character, "")
rescue IndexError
        self
end
reflect() click to toggle source
# File lib/hrules.rb, line 57
def reflect
        "#{self}#{self.reverse}"
end
replace_with_char(characters) click to toggle source
# 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_left() click to toggle source
# File lib/hrules.rb, line 61
def rotate_left
        self.split("").rotate(1).join("")
end
rotate_right() click to toggle source
# File lib/hrules.rb, line 65
def rotate_right
        self.split("").rotate(-1).join("")
end
swap(characters) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/hrules.rb, line 116
def truncate_from_x(position)
        self[0..position.to_i]
rescue IndexError
        self
end