Opposite of chomp in Ruby

There does not seem to be an existing function which does the opposite of ‘chomp’ in Ruby in the standard library.

If you’d like to use a function to remove a specific substring from the beginning of a string, try this core extension.  Note: This doesn’t do any magic with whitespace as chomp does.

class String
def remove_from_beginning(string_to_remove)
len = string_to_remove.size
if self[0..(len-1)] == string_to_remove
return self[len..-1]

Leave a Reply

Your email address will not be published. Required fields are marked *