Ruby中如何将嵌套散列转换成点分隔字符串的代码一行实现?
- 内容介绍
- 文章标签
- 相关推荐
本文共计433个文字,预计阅读时间需要2分钟。
要将YAML转换为点分隔的字符串,可以使用Ruby的`yaml`库来解析YAML内容,然后递归地遍历数据结构,将每个键和值转换为点分隔的格式。以下是一个简单的示例:
rubyrequire 'yaml'
def yaml_to_dotSeparated(yaml_str) data=YAML.safe_load(yaml_str) dot_separated(data, '')end
def dot_separated(data, prefix='') case data when Hash data.map do |key, value| dot_separated(value, #{prefix}#{key}.) end.join when Array data.map { |item| dot_separated(item, prefix) }.join else #{prefix}#{data} endend
示例YAML字符串yaml_str=<<-YAMLname: John Doeage: 30children: - name: Jane Doe age: 10YAML
转换YAML到点分隔的字符串puts yaml_to_dotSeparated(yaml_str)
这段代码首先定义了一个`yaml_to_dotSeparated`方法,它使用`YAML.safe_load`来解析YAML字符串。然后,它调用`dot_separated`方法来递归地将数据转换为点分隔的字符串。`dot_separated`方法根据数据类型(哈希、数组或值)来决定如何格式化输出。
WhatsthesimplestmethodtoconvertYAMLtodot-separatedstringsinRuby?在Ruby中,将YAML转换为点分隔字符串的最What's the simplest method to convert YAML to dot-separated strings in Ruby?
在Ruby中,将YAML转换为点分隔字符串的最简单方法是什么?
So this:
所以这个:
root: child_a: Hello child_b: nested_child_a: Nesting nested_child_b: Nesting Again child_c: KTo this:
:
{ "ROOT.CHILD_A" => "Hello", "ROOT.CHILD_B.NESTED_CHILD_A" => "Nesting", "ROOT.CHILD_B.NESTED_CHILD_B" => "Nesting Again", "ROOT.CHILD_C" => "K"}1 个解决方案
#1
12
It's not a one-liner, but perhaps it will fit your needs
这不是一句俏皮话,但也许它会适合你的需要
def to_dotted_hash(source, target = {}, namespace = nil) prefix = "#{namespace}." if namespace case source when Hash source.each do |key, value| to_dotted_hash(value, target, "#{prefix}#{key}") end when Array source.each_with_index do |value, index| to_dotted_hash(value, target, "#{prefix}#{index}") end else target[namespace] = source end targetendrequire 'pp'require 'yaml'data = YAML.load(DATA)pp datapp to_dotted_hash(data)__END__root: child_a: Hello child_b: nested_child_a: Nesting nested_child_b: Nesting Again child_c: Kprints
打印
{"root"=> {"child_a"=>"Hello", "child_b"=>{"nested_child_a"=>"Nesting", "nested_child_b"=>"Nesting Again"}, "child_c"=>"K"}} {"root.child_c"=>"K", "root.child_b.nested_child_a"=>"Nesting", "root.child_b.nested_child_b"=>"Nesting Again", "root.child_a"=>"Hello"}本文共计433个文字,预计阅读时间需要2分钟。
要将YAML转换为点分隔的字符串,可以使用Ruby的`yaml`库来解析YAML内容,然后递归地遍历数据结构,将每个键和值转换为点分隔的格式。以下是一个简单的示例:
rubyrequire 'yaml'
def yaml_to_dotSeparated(yaml_str) data=YAML.safe_load(yaml_str) dot_separated(data, '')end
def dot_separated(data, prefix='') case data when Hash data.map do |key, value| dot_separated(value, #{prefix}#{key}.) end.join when Array data.map { |item| dot_separated(item, prefix) }.join else #{prefix}#{data} endend
示例YAML字符串yaml_str=<<-YAMLname: John Doeage: 30children: - name: Jane Doe age: 10YAML
转换YAML到点分隔的字符串puts yaml_to_dotSeparated(yaml_str)
这段代码首先定义了一个`yaml_to_dotSeparated`方法,它使用`YAML.safe_load`来解析YAML字符串。然后,它调用`dot_separated`方法来递归地将数据转换为点分隔的字符串。`dot_separated`方法根据数据类型(哈希、数组或值)来决定如何格式化输出。
WhatsthesimplestmethodtoconvertYAMLtodot-separatedstringsinRuby?在Ruby中,将YAML转换为点分隔字符串的最What's the simplest method to convert YAML to dot-separated strings in Ruby?
在Ruby中,将YAML转换为点分隔字符串的最简单方法是什么?
So this:
所以这个:
root: child_a: Hello child_b: nested_child_a: Nesting nested_child_b: Nesting Again child_c: KTo this:
:
{ "ROOT.CHILD_A" => "Hello", "ROOT.CHILD_B.NESTED_CHILD_A" => "Nesting", "ROOT.CHILD_B.NESTED_CHILD_B" => "Nesting Again", "ROOT.CHILD_C" => "K"}1 个解决方案
#1
12
It's not a one-liner, but perhaps it will fit your needs
这不是一句俏皮话,但也许它会适合你的需要
def to_dotted_hash(source, target = {}, namespace = nil) prefix = "#{namespace}." if namespace case source when Hash source.each do |key, value| to_dotted_hash(value, target, "#{prefix}#{key}") end when Array source.each_with_index do |value, index| to_dotted_hash(value, target, "#{prefix}#{index}") end else target[namespace] = source end targetendrequire 'pp'require 'yaml'data = YAML.load(DATA)pp datapp to_dotted_hash(data)__END__root: child_a: Hello child_b: nested_child_a: Nesting nested_child_b: Nesting Again child_c: Kprints
打印
{"root"=> {"child_a"=>"Hello", "child_b"=>{"nested_child_a"=>"Nesting", "nested_child_b"=>"Nesting Again"}, "child_c"=>"K"}} {"root.child_c"=>"K", "root.child_b.nested_child_a"=>"Nesting", "root.child_b.nested_child_b"=>"Nesting Again", "root.child_a"=>"Hello"}
