Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yclim95/7839644a09d25d3e43f4 to your computer and use it in GitHub Desktop.
Save yclim95/7839644a09d25d3e43f4 to your computer and use it in GitHub Desktop.
def destroy_message!(string)
#TODO: remove the message from string destructively!
new_string=string[/[\W|\w]*:/] # '*:' is the whole thing b4 the * + inclusive of *
if string.include? ':'
string.sub!(string,new_string)
else
string
end
end
def destroy_message!(string)
#TODO: remove the message from string destructively!
new_string=string[/[\W|\w]*:/]
if string.include? ':'
string.!sub(string,new_string) # permanently modify the value
else
string
end
# Driver code...
string = "this message will self-destruct: you can't hug every cat"
original_string = string.dup
puts destroy_message(string) == "this message will self-destruct:"
puts string == original_string # we shouldn't modify the string passed to destroy_message
string = "this has no message"
original_string = string.dup
puts destroy_message(string) == string
puts string == original_string # we shouldn't modify the string passed to destroy_message
string = "this message will self-destruct: you can't hug every cat"
original_string = string.dup
destroy_message!(string)
puts string == "this message will self-destruct:"
puts string != original_string
string = "this has no message"
result = destroy_message!(string)
puts result.nil?
puts string == string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment