Forked from nextacademy-private/this_message_will_self_destruct.rb
Last active
January 12, 2016 14:30
-
-
Save yclim95/7839644a09d25d3e43f4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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