Class: StringJoiner

Inherits:
String
  • Object
show all
Defined in:
lib/string_joiner.rb,
lib/string_joiner/version.rb

Overview

does not override String#concat nor String#+

Defined Under Namespace

Modules: BlankToString

Constant Summary collapse

VERSION =
"0.1.0"

Instance Method Summary collapse

Constructor Details

#initialize(str = "", separator = ", ", allow_blanks: false, allow_leading_separator: false) ⇒ StringJoiner

Returns a new instance of StringJoiner.



16
17
18
19
20
21
22
23
# File 'lib/string_joiner.rb', line 16

def initialize(str = "", separator = ", ", allow_blanks: false, allow_leading_separator: false)
  str, separator = str.to_s, separator.to_s # liberal, allow symbols
  str = "" if !allow_blanks && str.blank?
  super(str)
  @separator = separator
  @allow_blanks = allow_blanks
  @allow_leading_separator = allow_leading_separator
end

Instance Method Details

#<<(str) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/string_joiner.rb', line 25

def <<(str)
  str = str.to_s # liberal, allow symbols
  
  return self if !@allow_blanks && str.blank? # don't add blanks if not allowed
  return replace(str) if !@allow_leading_separator && self == "" # don't allow leading comma nor replace with blank if not allowed

  super(@separator + str)
end

#to_sObject



34
35
36
# File 'lib/string_joiner.rb', line 34

def to_s
  String.new(self)
end