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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With