module RubymentStringsModule

# begin_documentation

This module offers functions to manipulate
Strings.

# end_documentation

Public Instance Methods

quoted_string__no_escaping(s) click to toggle source

returns a quoted version of a string; does not escape against quoting chars inside it.

# File lib/rubyment.rb, line 306
def quoted_string__no_escaping s
  "\"#{s}\""
end
quoted_string__no_escaping_single(s) click to toggle source

returns a quoted version of a string; does not escape against quoting chars inside it.

# File lib/rubyment.rb, line 315
def quoted_string__no_escaping_single s
  "\'#{s}\'"
end
string__from_definition(definition) click to toggle source

If definition cannot map, retuns itself.

Otherwise, it will generate a string out of the values given in that array, which can describe, for instance, recursive expressions. Better explained by examples:

Examples:

string__from_definition [ “value”, “var” ] # => “var value”

string__from_definition [ “value” ] # => “value”

string__from_definition [ “value”, [“var”], “=” ] # => “var=value”

string__from_definition [ “value”, [“var”], “” ] # => “varvalue”

string__from_definition [ nil, [“var”], “=” ] # => “var=”

string__from_definition [ “value”, [“var”, “EXPORT”], “=” ] # => “EXPORT var=value”

string__from_definition [ [“value”], [“var”, “EXPORT”], “=” ] # => “EXPORT var=value”

string__from_definition [“value”, nil, nil, nil, “`”, “`”] # => “`value`”

string__from_definition [ [“value”, nil, nil, nil, “`”, “`”] , [“var”, “EXPORT”], “=” ] # => “EXPORT var=`value`”

# —– test the spacer:

string__from_definition [ “value”, [“var”], “=”, “,” ] # => “var,=,value”

string__from_definition [ “value”, [“var”], nil, “,” ] # => “var, ,value”

string__from_definition [ “value”, [“var”], “”, “,” ] # => “var,value”

string__from_definition [ “ … describe here … ”, “contents:”, “”, “n”, “# begin section”, “# end section ”] # => “# begin sectionncontents:n … describe here … n# end section ”

# File lib/rubyment.rb, line 463
def string__from_definition definition
  return definition if (
    definition.respond_to?(:map).negate_me
  )

  value,
    variable,
    operation,
    spacer,
    open,
    close,
    reserved = definition.map { |d|
    send __method__, d # recursive call
  }

  # if variable is non empty, the default operation is "="
  # of course, only applied if operation is not set
  # A priori, nne not to be used, otherwise "" operation
  # won't be allowed
  operation ||= (variable && " " ||  "")
  spacer = spacer.nne ""
  variable  = variable.nne ""

  rv = string__recursive_join [
    spacer,  # separator
    open,
    [
      spacer, # separator
      variable,
      operation,
      value,
    ],
    close,
  ]

  rv

end
string__recursive_join(joinner_arrays_tuple) click to toggle source

Makes possible to join, similarly to Array#join, recursively.

Note of difference in behavior:

[ “”, “” ].join “+” # => “+”

[ nil, nil ].join “+” # => “+”

In this function, empty strings are not joinned (.nne and .compact are run before). It's a planned change to expand the interface to be able to instruct for not nne/compact strings/arrays before. TODO: via inheritable joinner; possibly indenter

Examples:

string__recursive_join [“+”, 1, 2, 3 ] # => “1+2+3”

string__recursive_join [ “; ”, [“+”, 1, 2, 3 ], [“-”, 2, 1] ] # => “1+2+3; 2-1”

string__recursive_join [ “; ”, [“+”, 1, [“/”, “a”, “b”], 3 ], [“-”, 2, 1] ] # => “1+a/b+3; 2-1”

# File lib/rubyment.rb, line 394
def string__recursive_join joinner_arrays_tuple
  return joinner_arrays_tuple if (
    joinner_arrays_tuple.respond_to?(:map).negate_me
  )

  joinner,
    *operands = joinner_arrays_tuple
  operands.map! { |operand|
    (
      send __method__, operand # recursive call
    ).nne
  }

  operands.compact.join(joinner)
end
strings__product(*strings) click to toggle source

Function that can generate distribution or combinations of an array of strings.

Closed for extension

examples: strings__product [ “model 1”, “model 2”], [“:”], [“variant with sound system”, “variant no sound system”], [“ ”], [“and stabilizer”, “and without stabilizer”] # => [“model 1:variant with sound system and stabilizer”, # “model 1:variant with sound system and without stabilizer”, # … # “model 2:variant no sound system and stabilizer”, # “model 2:variant no sound system and without stabilizer”]

# File lib/rubyment.rb, line 336
def strings__product *strings
  arrays = arrays__product *strings
  invoke__basic_sender_array [ arrays, :map  ], &:join
end
strings__zip(*strings) click to toggle source

Function that can generate a match of an array of strings.

Closed for extension

examples:

# compare the results below, with calling strings__product [ “model 1”, “model 2”], [“:”], [“variant with sound system”, “variant no sound system”], [“ ”], [“and stabilizer”, “and without stabilizer”]

strings__zip [ “model 1”, “model 2”], [“:”], [“variant with sound system”, “variant no sound system”], [“ ”], [“and stabilizer”, “and without stabilizer”] # => [“model 1:variant with sound system and stabilizer”, # “model 2variant no sound systemand without stabilizer”]

# File lib/rubyment.rb, line 357
def strings__zip *strings
  arrays = arrays__zip *strings
  invoke__basic_sender_array [ arrays, :map  ], &:join
end