简体   繁体   English

如何将符号转换为字符串(即带状开头:) ruby​​ to_yaml

[英]How to convert symbols to strings (i.e. strip leading :) ruby to_yaml

Am trying to remove the leading : from my YAML output. 我正在尝试从我的YAML输出中删除前导: Here is the code and what I have done below: 这是代码,下面是我做的:

model/attribution_channel.rb 模型/ attribution_channel.rb

DEFAULT_BONUS_CONFIG =  {
  sign_up: {
    currency: 'ngn',
    type: 'flat',
    amount: 1000
  },
  visit: {
    currency: 'ngn',
    type: 'flat',
    amount: 5
  }
}

view/form.slim.html 视图/ form.slim.html

AttributionChannel::DEFAULT_BONUS_CONFIG.to_yaml

The Output: 输出:

产量

To remove the YAML Separator --- and the Leading : in the keys from my output, here is what I have done: 要从输出中删除YAML分隔符---Leading:,请执行以下操作:

AttributionChannel::DEFAULT_BONUS_CONFIG.to_yaml.gsub("---\n", '').sub(":", '')

..but the .sub(":", '') part removed only the : of the first leading : . ..但该.sub(":", '')部分仅除去:第一领先的:

How do I remove the leading : from my YAML output? 如何从YAML输出中删除前导: Any help is appreciated? 任何帮助表示赞赏吗? Here is what I want below: 这是我想要的以下内容:

sign_up:
  currency: ngn
  type: flat
  amount: 1000
visit:
  currency: ngn
  type: flat
  amount: 5

You need to have keys as strings to skip : generation 您需要将键作为字符串跳过:生成

require 'active_support/core_ext/hash/keys'
require 'yaml'

DEFAULT_BONUS_CONFIG.deep_stringify_keys.to_yaml.gsub("---\n", '')

 => "sign_up:\n  currency: ngn\n  type: flat\n  amount: 1000\nvisit:\n  currency: ngn\n  type: flat\n  amount: 5\n"

You can convert hash keys to strings before generating YAML. 您可以在生成YAML之前将哈希键转换为字符串。 The code below goes through a hash recursively convering every key to hash and stringifying every value if it's a hash (note that it's not prepared for circular dependencies in hash). 下面的代码经过哈希处理,将每个键递归地转换为哈希,如果是哈希,则对每个值进行字符串化处理(请注意,它没有为哈希中的循环依赖做好准备)。

def stringify(hash)
  hash.map{|k, v| [k.to_s, v.is_a?(Hash) ? stringify(v) : v] }.to_h
end  

puts stringify(DEFAULT_BONUS_CONFIG).to_yaml

---
sign_up:
  currency: ngn
  type: flat
  amount: 1000
visit:
  currency: ngn
  type: flat
  amount: 5

EDIT: Regarding the --- at the beginning, see this answer . 编辑:关于---一开始,请参阅此答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何防止Ruby的.to_yaml使用引用? - How can I prevent Ruby's .to_yaml from using references? 为什么在Ruby中使用to_yaml会出现NoMethodError - Why I get NoMethodError by using to_yaml in Ruby 如何在Rails中使用to_yaml方法,并删除'---' - How to use to_yaml method in Rails, and remove '---' 如何使这种Ruby方法更具“红宝石风格”-即DRY和时尚? - How do I make this Ruby method more 'ruby-esque' - i.e. DRY and sleek? 在Rails应用程序中使用mongodb备份中的数据…即使用ruby将BSON转换为JSON? - Use data from mongodb backup in rails app… i.e. convert BSON to JSON using ruby? 将哈希的第一位将语言环境名称转换为纯文本,即en_GB转换为Rails中的ruby - Convert the first bit of the hash the locale name to plain text i.e. en_GB to Great Britain in ruby on rails 如何在Ruby 2.3.0中将字符串转换为YAML? - How to convert string to YAML in Ruby 2.3.0? 使用ruby on rails和devise,如何创建另一个刚注册用户的模型实例(即第一次)? - With ruby on rails and devise, how to create another model instance belonging to a user that has just registered (i.e. for the first time)? 我将数据转储到yml文件,但无法加载。 是“ to_yaml”错误吗? - I dump data to a yml file, but can't load. Is it a “to_yaml” bug? 如何按月获取帖子摘要? 即月数 - How to get a summary of Posts by month? i.e. month with count
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM