Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/everythingrb/core/string.rb

Overview

Extensions to Ruby’s core String class

These additions make working with JSON strings easy by providing methods for conversion to various Ruby data structures. Plus some nice formatting helpers that saves tons of typing

Examples:

Converting JSON to different structures

json = '{"user": {"name": "Alice", "admin": true}}'
json.to_h                 # => {user: {name: "Alice", admin: true}}
json.to_istruct.user.name # => "Alice"
json.to_ostruct.user.name # => "Alice"

Instance Method Summary collapse

Instance Method Details

#to_deep_hHash

Deep parsing of nested JSON strings Recursively attempts to parse string values as JSON

Examples:

nested_json = '{
  "user": "{\"name\":\"Alice\",\"roles\":[\"admin\"]}"
}'
nested_json.to_deep_h
# => {user: {name: "Alice", roles: ["admin"]}}

Returns:

  • (Hash)

    Deeply parsed hash with all nested JSON strings converted



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/everythingrb/core/string.rb', line 46

def to_deep_h
  recursive_convert = lambda do |object|
    case object
    when Array
      object.map { |v| recursive_convert.call(v) }
    when String
      result = object.to_deep_h

      # Nested JSON
      if result.is_a?(Array) || result.is_a?(Hash)
        recursive_convert.call(result)
      else
        object
      end
    when Hash
      object.transform_values { |v| recursive_convert.call(v) }
    else
      object
    end
  end

  recursive_convert.call(to_h)
end

#to_hHash? Also known as: to_a

Converts JSON string to Hash, returning nil if it failed

Examples:

'{"name": "Alice"}'.to_h  # => {name: "Alice"}
"invalid json".to_h       # => nil

Returns:

  • (Hash, nil)

    Parsed JSON as hash or nil if invalid JSON



25
26
27
28
29
# File 'lib/everythingrb/core/string.rb', line 25

def to_h
  JSON.parse(self, symbolize_names: true)
rescue JSON::ParserError
  nil
end

#to_istructData?

Attempts to parse JSON and convert to Data struct. Returns nil if string does not contain valid JSON

Examples:

'{"name": "Alice"}'.to_istruct      # => #<data name="Alice">
"not json".to_istruct               # => nil

Returns:

  • (Data, nil)

    Immutable Data structure or nil if invalid JSON



80
81
82
# File 'lib/everythingrb/core/string.rb', line 80

def to_istruct
  to_h&.to_istruct
end

#to_ostructOpenStruct?

Attempts to parse JSON and convert to OpenStruct. Returns nil if string does not contain valid JSON

Examples:

'{"name": "Alice"}'.to_ostruct      # => #<OpenStruct name="Alice">
"not json".to_ostruct               # => nil

Returns:

  • (OpenStruct, nil)

    OpenStruct or nil if invalid JSON



94
95
96
# File 'lib/everythingrb/core/string.rb', line 94

def to_ostruct
  to_h&.to_ostruct
end

#to_structStruct?

Attempts to parse JSON and convert to Struct. Returns nil if string does not contain valid JSON

Examples:

'{"name": "Alice"}'.to_struct       # => #<struct name="Alice">
"not json".to_struct                # => nil

Returns:

  • (Struct, nil)

    Struct or nil if invalid JSON



108
109
110
# File 'lib/everythingrb/core/string.rb', line 108

def to_struct
  to_h&.to_struct
end

#with_quotesString Also known as: in_quotes

Returns self wrapped in double quotes

Examples:

"Hello World".with_quotes           # => "\"Hello World\""
"Quote \"me\"".with_quotes          # => "\"Quote \\\"me\\\"\""

Returns:

  • (String)

    The string with surrounding double quotes



121
122
123
# File 'lib/everythingrb/core/string.rb', line 121

def with_quotes
  %("#{self}")
end