<3 Ruby

Today in #rubyonrails someone asked if there was a convenient way to append something (like a /) to a string if it wasn't already there. I couldn't think of one, so I wrote one:

class String
  def append_unless_ends_with!(string)
    self[-string.length..-1] == string ? self : self < < string
  end
end

I love ruby. :)

Comments