Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Process nested hash to convert all values to strings

I have the following code which takes a hash and turns all the values in to strings.

def stringify_values obj
  @values ||= obj.clone

  obj.each do |k, v|
    if v.is_a?(Hash)
      @values[k] = stringify_values(v)
    else
      @values[k] = v.to_s
    end
  end

  return @values
end

So given the following hash:

{
  post: {
    id: 123,
    text: 'foobar',
  }
}

I get following YAML output

--- &1
:post: *1
:id: '123'
:text: 'foobar'

When I want this output

--- 
:post: 
  :id: '123'
  :text: 'foobar'

It looks like the object has been flattened and then been given a reference to itself, which causes Stack level errors in my specs.

How do I get the desired output?

like image 364
Cjmarkham Avatar asked Dec 19 '22 20:12

Cjmarkham


1 Answers

A simpler implementation of stringify_values can be - assuming that it is always a Hash. This function makes use of Hash#deep_merge method added by Active Support Core Extensions - we merge the hash with itself, so that in the block we get to inspect each value and call to_s on it.

def stringify_values obj
  obj.deep_merge(obj) {|_,_,v| v.to_s}
end

Complete working sample:

require "yaml"
require "active_support/core_ext/hash"

def stringify_values obj
  obj.deep_merge(obj) {|_,_,v| v.to_s}
end

class Foo
    def to_s
        "I am Foo"
    end
end

h = {
  post: {
    id: 123,
    arr: [1,2,3],
    text: 'foobar',
    obj: { me: Foo.new}
  }
}

puts YAML.dump (stringify_values h)
#=> 
---
:post:
  :id: '123'
  :arr: "[1, 2, 3]"
  :text: foobar
  :obj:
    :me: I am Foo

Not sure what is the expectation when value is an array, as Array#to_s will give you array as a string as well, whether that is desirable or not, you can decide and tweak the solution a bit.

like image 105
Wand Maker Avatar answered Jan 21 '23 08:01

Wand Maker