简体   繁体   English

Prettyprint将ruby hash转换为erlang术语格式

[英]Prettyprint a ruby hash to erlang term format

I am trying to write a configuration file for an erlang application (rabbitmq) whose conf si written as an erlang term 1 . 我正在尝试为配置为erlang术语1的erlang应用程序(rabbitmq)编写配置文件。

My attribute is a ruby hash, do you know how I can convert this hash to a pretty printed erlang term ? 我的属性是红宝石哈希,您知道如何将哈希转换为漂亮的印刷erlang术语吗?

If I had to do this on my own, maybe something like: 如果我必须自己做,可能是这样的:

def to_erl(o)
  case o
  when Hash
    '[' +
       o.map {|(k,v)|
        "{#{k}, #{to_erl v}}"
       }.join(",\n") +
     ']'
  when Array
    '[' + o.map{|v| to_erl(v)}.join(",")  +']'
  when TrueClass then "true"
  when FalseClass then "false"
  when Integer then o.to_s
  when String then o
  when Symbol then o.to_s
  # ... and whatever else you can think of
  else
    raise "Don't know how to erlify #{o}"
  end
end

... But I bet there's some nice code you can steal off of some open source project that has to serialize between ruby and erlang. ...但是我敢打赌,您可以从一些必须在ruby和erlang之间进行序列化的开源项目中窃取一些不错的代码。

For serialization to erlang , BERT is the defacto (Binary Erlang terms). 对于序列化为erlang ,BERT是事实上的(二进制Erlang术语)。 There's lots of libraries that go from Ruby hashes to BERT, it seems. 似乎有很多从Ruby哈希表到BERT的库。 Haven't seen one that gives you plain textual erlang terms. 尚未看到能给您纯文本的erlang术语的语言。

Since this is for a chef recipe, it's a good idea not to mess with too many ruby dependencies at converge time. 因为这是厨师的食谱,所以最好不要在收敛时弄乱太多的红宝石依赖关系。 You'll be calling #to_hash on your chef attributes, and what you will get back will straight-forward enough (no objects, just number/bools/strings/arrays and hashes I guess) to manage with a bit of home grown code. 您将在Chef属性上调用#to_hash ,您将获得的返回值将很简单(我猜没有对象,只有数字/布尔值/字符串/数组和哈希),可以使用一些本地代码进行管理。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM