Skip to content

Instantly share code, notes, and snippets.

@philips
Created August 28, 2012 00:42
Show Gist options
  • Save philips/3493889 to your computer and use it in GitHub Desktop.
Save philips/3493889 to your computer and use it in GitHub Desktop.
# This function replaces any portion of a string and returns an identically
# sized string. How might the sanity tests at the top fail to return an
# identical sized string.
#
# Where else is it broken?
#
# How would you test it?
def slice_and_transform(str, loc, len, rep)
if (loc < 1)
return nil
end
if (loc + len > str.length)
return nil
end
str.slice!(loc, len)
return str + rep
end
str = "Hello Earth"
loc = 6
rep = "Venus"
puts(slice_and_transform(str, loc, rep.length, rep))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment