Professional Web Applications Themes

YAML and quoted strings - Ruby

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. TIA -- Marek Janukowicz...

  1. #1

    Default YAML and quoted strings


    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.

    TIA
    --
    Marek Janukowicz
    Marek Guest

  2. #2

    Default Re: YAML and quoted strings

    In article <ha.pwr.wroc.pl>,
    Marek Janukowicz <ds.pwr.wroc.pl> wrote:
     

    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"

    Without a little more context it is difficult to go much further.

    Hope this helps,

    Mike

    --
    co.uk | The "`Stok' disclaimers" apply.
    http://www.stok.co.uk/~mike/ | GPG PGP Key 1024D/059913DA
    com | Fingerprint 0570 71CD 6790 7C28 3D60
    http://www.exegenix.com/ | 75D2 9EC4 C1C0 0599 13DA
    Mike Guest

  3. #3

    Default Re: YAML and quoted strings

    On Sun, 15 Feb 2004 16:13:01 GMT, Mike Stok wrote: 
    >
    > 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"[/ref]

    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
    Marek Guest

  4. #4

    Default Re: YAML and quoted strings

    In article <ha.pwr.wroc.pl>,
    Marek Janukowicz <ds.pwr.wroc.pl> wrote:
     

    Well, YAML is constrained to have some structure, and you could use a
    trimmed block e.g.

    #!/usr/bin/env ruby

    require 'yaml'

    s = YAML.load(<<ETX)
    ---
    key1:
    - |-
    'foo'
    - |-
    "bar"
    - |-
    "O'Brien"
    ETX

    puts "got #{s.inspect}"

    which produces got {"key1"=>["'foo'", "\"bar\"", "\"O'Brien\""]}

    .... but here you have to be careful with indentation, and you still have
    to be aware of what's significant to YAML. The section about blocks in
    http://yaml4r.sourceforge.net/cookbook/ may be useful as there are
    variations on | and > which may suit your purpose better than |-.

    Hope this helps,

    Mike
    --
    co.uk | The "`Stok' disclaimers" apply.
    http://www.stok.co.uk/~mike/ | GPG PGP Key 1024D/059913DA
    com | Fingerprint 0570 71CD 6790 7C28 3D60
    http://www.exegenix.com/ | 75D2 9EC4 C1C0 0599 13DA
    Mike Guest

  5. #5

    Default Re: YAML and quoted strings

    Marek Janukowicz wrote:
     

    Next release of Syck will distinguish between plain and quoted strings.
    The YAML specification recently enforces this distinction.

    Can you give me specifics on your needs? I have ideas for an API, but
    I'd like to hear your suggestions.

    _why



    why Guest

  6. #6

    Default Re: YAML and quoted strings

    On Mon, 16 Feb 2004 03:53:43 +0900, why the lucky stiff wrote: 
    >
    > Next release of Syck will distinguish between plain and quoted strings.
    > The YAML specification recently enforces this distinction.[/ref]

    Glad to hear that.
     

    My needs are difficult to explain, but in short: I just need to be able
    to distinguish between quoted and unquoted string because I am writing a
    library which has different semantics for these 2 kinds of strings. The
    YAML files will be created by the developer (user of the library), so
    I'd like them as readable and clear as possible and I don't think having
    to write "'string'" complies to these conditions.

    --
    Marek Janukowicz
    Marek Guest

  7. #7

    Default Re: YAML and quoted strings

    Marek Janukowicz wrote:
     
    >My needs are difficult to explain, but in short: I just need to be able
    >to distinguish between quoted and unquoted string because I am writing a
    >library which has different semantics for these 2 kinds of strings. The
    >YAML files will be created by the developer (user of the library), so
    >I'd like them as readable and clear as possible and I don't think having
    >to write "'string'" complies to these conditions.
    >
    >[/ref]
    It sounds a bit like a special case. Perhaps the best approach would be
    to decide on what form you prefer, and write a translator that will
    convert that form into a YAML form?




    Charles Guest

Similar Threads

  1. Replies: 2
    Last Post: December 4th, 04:20 PM
  2. #25281 [Fbk->Opn]: "$foo->bar->baz" ambiguity in double quoted strings
    By phpbugs at rickbradley dot com in forum PHP Development
    Replies: 0
    Last Post: August 28th, 05:58 AM
  3. split operator with quoted strings
    By arasu@nospam.org in forum PERL Miscellaneous
    Replies: 3
    Last Post: July 11th, 12:43 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139