Module: Nandi::Formatting

Defined Under Namespace

Modules: ClassMethods Classes: UnsupportedValueError

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



48
49
50
# File 'lib/nandi/formatting.rb', line 48

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#format_value(value, opts = {}) ⇒ Object

Create a string representation of the value that is a valid and correct Ruby literal for that value. Please note that the exact representation is not guaranteed to be the same between different platforms and Ruby versions. The guarantee is merely that calling this method with any supported type will produce a string that produces an equal value when it is passed to Kernel::eval

Parameters:

  • value (Hash, Array, String, Symbol, Integer, Float, NilClass)

    value to format



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/nandi/formatting.rb', line 34

def format_value(value, opts = {})
  case value
  when Hash
    format_hash(value, opts)
  when Array
    "[#{value.map { |v| format_value(v, opts) }.join(', ')}]"
  when String, Symbol, Integer, Float, NilClass, TrueClass, FalseClass
    value.inspect
  else
    raise UnsupportedValueError,
          "Cannot format value of type #{value.class.name}"
  end
end