如何将ruby哈希转换为具有特定格式的字符串
这是我要格式化的哈希如何将ruby哈希转换为具有特定格式的字符串,ruby,Ruby,这是我要格式化的哈希 input = {"test_key"=>"test_value", "test_key2"=>"test_value2"} 这是预期的结果 "{\n\t\"test_key\" = \"test_value\";\n\t\"test_key2\" = \"test_value2\";\n}" 到目前为止,我有以下代码 def format_hash(hash) output = "" hash.to_s.split(',').each do |k|
input = {"test_key"=>"test_value", "test_key2"=>"test_value2"}
这是预期的结果
"{\n\t\"test_key\" = \"test_value\";\n\t\"test_key2\" = \"test_value2\";\n}"
到目前为止,我有以下代码
def format_hash(hash)
output = ""
hash.to_s.split(',').each do |k|
new_string = k + ';'
new_string.gsub!('=>', ' = ')
output += new_string
end
end
这给了我这个输出
output = "{\"test_key\" = \"test_value\"; \"test_key2\" = \"test_value2\"};"
但我仍在努力添加其余部分。有什么想法/建议吗?一个起点可能是使用JSON格式化程序:
require 'json'
input = {"test_key"=>"test_value", "test_key2"=>"test_value2"}
JSON.pretty_generate(input)
=> "{\n \"test_key\": \"test_value\",\n \"test_key2\": \"test_value2\"\n}"
def formatter(hash)
output = ""
output += "{\n\t"
output += hash.entries.map{|a| "\"#{a[0]}\" = \"#{a[1]}\"" }.join(";\n\t")
output += ";\n}"
end
formatter( input )
这有一些细微的区别,因为看起来您使用的是=
,而不是:
。这就是说,也许从这个工作比从你拥有的工作更容易
使用JSON
自定义格式化程序
当然,您可以定义自定义格式化程序:
require 'json'
input = {"test_key"=>"test_value", "test_key2"=>"test_value2"}
JSON.pretty_generate(input)
=> "{\n \"test_key\": \"test_value\",\n \"test_key2\": \"test_value2\"\n}"
def formatter(hash)
output = ""
output += "{\n\t"
output += hash.entries.map{|a| "\"#{a[0]}\" = \"#{a[1]}\"" }.join(";\n\t")
output += ";\n}"
end
formatter( input )
一个起点可能是使用JSON格式化程序:
require 'json'
input = {"test_key"=>"test_value", "test_key2"=>"test_value2"}
JSON.pretty_generate(input)
=> "{\n \"test_key\": \"test_value\",\n \"test_key2\": \"test_value2\"\n}"
def formatter(hash)
output = ""
output += "{\n\t"
output += hash.entries.map{|a| "\"#{a[0]}\" = \"#{a[1]}\"" }.join(";\n\t")
output += ";\n}"
end
formatter( input )
这有一些细微的区别,因为看起来您使用的是=
,而不是:
。这就是说,也许从这个工作比从你拥有的工作更容易
使用JSON
自定义格式化程序
当然,您可以定义自定义格式化程序:
require 'json'
input = {"test_key"=>"test_value", "test_key2"=>"test_value2"}
JSON.pretty_generate(input)
=> "{\n \"test_key\": \"test_value\",\n \"test_key2\": \"test_value2\"\n}"
def formatter(hash)
output = ""
output += "{\n\t"
output += hash.entries.map{|a| "\"#{a[0]}\" = \"#{a[1]}\"" }.join(";\n\t")
output += ";\n}"
end
formatter( input )
“{”[“\n\t\“test\u key\”=“test\u value\”,“\n\t\“test\u key2\”=“test\u value2\”]
b=a.join(“;”)
#=>“\n\t\”测试密钥“=”测试值“;\n\t\”测试密钥2“=”测试值2“
"{"
“{”[“\n\t\“test\u key\”=“test\u value\”,“\n\t\“test\u key2\”=“test\u value2\”]
b=a.join(“;”)
#=>“\n\t\”测试密钥“=”测试值“;\n\t\”测试密钥2“=”测试值2“
“{”知道我为什么在输出中得到+
符号吗?我正在使用rails控制台“{\n”+“\t\“test\u key\”=“test\u value\”;\n“+”\t\“test\u key 2\”=“test\u value2\”;\n”+“}”
你确定你不是在看你输入的内容而不是返回值吗?因为你尝试了,我将+
改为,谢谢你的绿色提示,但最好等一等,主要是为了避免阻止其他读者发布答案,同时也为了让仍在编写答案的读者完成答案。这里的大多数读者在做出选择之前至少需要几个小时;有些人会等到世界各地熟睡的读者都会选择。不必着急。如果你愿意的话,可以随意取消复选标记。知道我为什么在输出中得到+
符号吗?我正在使用rails控制台“{\n”+“\t\“test\u key\”=“test\u value\”;\n+“\t\“test\u key2\”=“test\u value2\”;\n“+”}”
你确定你不是在看你输入的内容而不是返回值吗?因为你尝试了,我将+
改为,谢谢你的绿色提示,但最好等一等,主要是为了避免阻止其他读者发布答案,同时也为了让仍在编写答案的读者完成答案。这里的大多数读者在做出选择之前至少需要几个小时;有些人会等到全世界熟睡的读者都调到了这样的状态。不必着急。如果你愿意的话,可以随意撤销复选标记。