class String

Public Instance Methods

to_h() click to toggle source
# File lib/str_to_hash.rb, line 2
def to_h
  if self[0] != "{" || self[-1] != "}"
    raise ArgumentError.new("invalid value for `#{__method__}': '#{self}'")
  end

  begin
    str = self.chomp.gsub(/"|^{|}$/, '')
    arr = str.split(/,[\s]*|=>/)
    arr.each_with_index{|column, idx|
      arr[idx] = column.to_i if column =~ /^\d+$/
    }
    new_style_res = Hash[*arr]

    new_style_res
  rescue
    raise ArgumentError.new("invalid value for `#{__method__}': '#{self}'")
  end
end