Commit 3b168b1f authored by Jamie Tanna's avatar Jamie Tanna

Document "ruby convert hash to string"

Closes #898.
parent edc5e100
Pipeline #115662233 passed with stages
in 7 minutes and 26 seconds
---
title: "Converting a Ruby Hash to a String"
description: "The ways that we can convert a Hash to a String with Ruby."
tags:
- blogumentation
- ruby
license_code: Apache-2.0
license_prose: CC-BY-NC-SA-4.0
date: 2020-02-06T19:24:39+0000
slug: "ruby-hash-string"
---
I noticed today a couple of visitors to the site who were searching for "ruby convert hash to string" - this article is for them, but also for me in the future when I forget!
One option, would be to use `.to_s` which converts anything in Ruby to its internal String representation.
This works if you want to have a representation output to a Ruby for use with copy-pasting into source code:
```ruby
hash_str = {'key'=> [1,2,3]}
puts hash_str.to_s
# {"key"=>[1, 2, 3]}
hash_sym = {key: [1,2,3]}
puts hash_sym.to_s
# {:key=>[1, 2, 3]}
```
But this won't quite work for folks that want to have a format that can be used in i.e. other languages or tools. For this, I would recommend [pretty-printing it as JSON]({{< ref 2018-06-18-pretty-printing-json-ruby-cli >}}):
```ruby
require 'json'
hash_str = {'key'=> [1,2,3]}
puts JSON.pretty_generate(hash_str)
# {
# "key": [
# 1,
# 2,
# 3
# ]
# }
hash_sym = {key: [1,2,3]}
puts JSON.pretty_generate(hash_sym)
# {
# "key": [
# 1,
# 2,
# 3
# ]
# }
```
---
posts:
- https://www.jvt.me/posts/2020/02/06/ruby-hash-string/
- https://www.jvt.me/posts/2020/02/03/week-notes-5/
- https://www.jvt.me/posts/2020/02/03/generate-pojo-xml-xsd/
- https://www.jvt.me/posts/2020/01/26/week-notes-4/
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment