From: Marek Janukowicz Date: 2004-02-16T02:19:53+09:00 Subject: Re: YAML and quoted strings On Sun, 15 Feb 2004 16:13:01 GMT, Mike Stok wrote: >>How can I store/load a quoted string using YAML? The problem is, YAML >>removes quotes when loading a file and I need to know whether the string >>was quoted or not. The only way I have found so far is to use "'some >>string'", but this is a bit ugly for me. > > It depends what you mean by a quoted string. If you mean a string > containing quotes then YAML handles them just fine, for instance > > #!/usr/bin/env ruby > > require 'yaml' > > FILE_NAME = 'test.yml' > > hash = { > 'key1' => %w{ 'foo' "bar" O'Brien } > } > > puts "storing: #{hash.inspect}" > > File.open(FILE_NAME, 'w') do |f| > f << hash.to_yaml > end > > File.open(FILE_NAME) do |f| > retrieved = YAML.load(f) > puts "retrieved: #{retrieved.inspect}" > puts "test element: #{retrieved['key1'][0]}" > end > > produces this output: > > storing: {"key1"=>["'foo'", "\"bar\"", "O'Brien"]} > retrieved: {"key1"=>["'foo'", "\"bar\"", "O'Brien"]} > test element: 'foo' > > and the content of test.yml is > > --- > key1: > - "'foo'" > - "\"bar\"" > - "O'Brien" Yes, I know YAML can handle string containing quotes. But my YAML files are to be created by a user by hand and I find the "'foo'" notation ugly. OTOH maybe I just expect too much... -- Marek Janukowicz