Module: Corefines::String::Concat

Defined in:
lib/corefines/string.rb

Instance Method Summary collapse

Instance Method Details

#concat!(obj, separator = nil) ⇒ String

Appends (concatenates) the given object to str. If the separator is set and this str is not empty, then it appends the separator before the obj.

Examples:

"".concat!("Greetings", ", ") # => "Greetings"
"Greetings".concat!("programs!", ", ") #=> "Greetings, programs!"

Parameters:

  • obj (String, Integer)

    the string, or codepoint to append.

  • separator (String, nil) (defaults to: nil)

    the separator to append when this str is not empty.

Returns:


161
162
163
164
165
166
167
168
169
170
171
# File 'lib/corefines/string.rb', line 161

module Concat
  refine ::String do
    def concat!(obj, separator = nil)
      if separator && !self.empty?
        self << separator << obj
      else
        self << obj
      end
    end
  end
end