to-yaml

Can Ruby's YAML module be used to embed comments?

淺唱寂寞╮ 提交于 2020-01-02 05:12:32
问题 The to_yaml method produces nice YAML output, but I would like to include comment lines before some of the elements. Is there a way to do so? For example, I would like to produce: # hostname or IP address of client client: host4.example.com # hostname or IP address of server server: 192.168.222.222 From something similar to: { :client => 'host4.example.com', :server => '192.168.222.222', }.to_yaml ... but am not sure if the YAML module even has a way to accomplish. UPDATE: I ended up not

Rails console 'y' helper returns NameError rather than yaml-formatting output

本小妞迷上赌 提交于 2019-12-17 22:54:07
问题 I'm trying to use y object in Rails 3.2.6/Ruby 1.9.3 console to get nicely formatted yaml output for an ActiveRecord object, but for some reason it isn't working for me. I've used it in the past, but somewhere along the way it broke. I get the following output when I try: NameError: undefined local variable or method `yaml' for main:Object 回答1: The y method is actually an extension to the Kernel object put in place by the Syck YAML parser/emitter. Here are the last few lines of lib/ruby/1.9.1

Ruby to_yaml utf8 string

北战南征 提交于 2019-12-04 13:55:17
问题 How can I make ruby to_yaml method to store utf8 strings with original signs but not escape sequence? 回答1: This is probably a really bad idea as I'm sure YAML has its reasons for encoding the characters as it does, but it doesn't seem too hard to undo: require 'yaml' require 'yaml/encoding' text = "Ça va bien?" puts text.to_yaml(:Encoding => :Utf8) # => --- "\xC3\x87a va bien?" puts YAML.unescape(YAML.dump(text)) # => --- "Ça va bien?" 回答2: require 'yaml' YAML::ENGINE.yamler='psych' 'Résumé'

Ruby to_yaml utf8 string

走远了吗. 提交于 2019-12-03 08:54:29
How can I make ruby to_yaml method to store utf8 strings with original signs but not escape sequence? This is probably a really bad idea as I'm sure YAML has its reasons for encoding the characters as it does, but it doesn't seem too hard to undo: require 'yaml' require 'yaml/encoding' text = "Ça va bien?" puts text.to_yaml(:Encoding => :Utf8) # => --- "\xC3\x87a va bien?" puts YAML.unescape(YAML.dump(text)) # => --- "Ça va bien?" require 'yaml' YAML::ENGINE.yamler='psych' 'Résumé'.to_yaml # => "--- Résumé\n...\n" Ruby ships with two YAML engines: syck and psych. Syck is old and not maintained

Rails console 'y' helper returns NameError rather than yaml-formatting output

梦想的初衷 提交于 2019-11-28 20:42:19
I'm trying to use y object in Rails 3.2.6/Ruby 1.9.3 console to get nicely formatted yaml output for an ActiveRecord object, but for some reason it isn't working for me. I've used it in the past, but somewhere along the way it broke. I get the following output when I try: NameError: undefined local variable or method `yaml' for main:Object The y method is actually an extension to the Kernel object put in place by the Syck YAML parser/emitter . Here are the last few lines of lib/ruby/1.9.1/syck.rb : module Kernel def y( object, *objects ) objects.unshift object puts( if objects.length == 1 YAML