2015-11-29 12 views
8

soru işareti hakkında bir paragraf 2.11 vardır:YAML'de karmaşık bir haritalama anahtarı nedir? YAML <a href="http://www.yaml.org/spec/1.2/spec.html" rel="noreferrer">specs</a> günü

bir soru işareti ve uzay (“?”) Karmaşık bir haritalama anahtarını göstermektedir. Blok koleksiyonunda, anahtar: değer çiftleri tire, kalın çizgi veya soru işaretini hemen izlemeye başlayabilir.

Bu örnek verilir:

--- 
? - Detroit Tigers 
    - Chicago cubs 
: 
    - 2001-07-23 

Bu, diğer örnek de başarısız XML dönüştürülmesini:

%YAML 1.2 
- - - 
!!map { 
    ? !!str "Not indented" 
    : !!map { 
     ? !!str "By one space" 
     : !!str "By four\n spaces\n", 
     ? !!str "Flow style" 
     : !!seq [ 
      !!str "By two", 
      !!str "Also by two", 
      !!str "Still by two", 
     ] 
    } 
} 

Ne yazık ki ne anlama geldiğini yapar anlamıyorum. Bunu codebeautify kullanarak XML'e dönüştürmeyi denedim, ancak bir hata alıyorum.

Yani benim soru:

soru işareti ne yapar?

cevap

23

Bu özellik çok net değil, ama biraz kafa çizdikten sonra anladım. YAML iki tür blok haritalama anahtarına sahiptir: örtülü ve açık. Ama aynı zamanda

{ "mapping" => 
    { "foo"  => 1, 
    "bar baz" => 2, 
    "qux:quux" => 3 
    } 
} 

kullanabilirsiniz:

mapping: 
    foo: 1 
    bar baz: 2 
    "qux:quux": 3 

biz şu sonucu olsun (örneğin) Ruby bu YAML yüklerseniz: Örtülü tarzı aşina olduğunuz türüdür açık tarzı aynı şeyi ifade etmek: başka bir deyişle

mapping: 
    ? foo 
    : 1 
    ? bar baz 
    : 2 
    ? "qux:quux" 
    : 3 

, ? başlayan bir çizgi bir anahtarı belirtir ve bir çizgiile başlayanbir değeri gösterir.

Bu ne işe yarar? Peki, YAML yapısını bir eşleme anahtarı olarak kullanmamıza izin veriyor. Bir diziyi bir eşleme anahtarı olarak kullanmak ister misiniz? Yapabilirsin! Bir eşleme eşleme anahtarı olarak kullanmak ister misiniz? Bakın:

YAML.load <<YML 
? - Detroit Tigers 
    - Chicago cubs 
: 
    - 2001-07-23 

? [ New York Yankees, 
    Atlanta Braves ] 
: [ 2001-07-02, 2001-08-12, 
    2001-08-14 ] 
YML 
# => { [ "Detroit Tigers", "Chicago cubs" ] => 
#   [ #<Date: 2001-07-23 ((2452114j,0s,0n),+0s,2299161j)> ], 
#  [ "New York Yankees", "Atlanta Braves" ] => 
#   [ #<Date: 2001-07-02 ((2452093j,0s,0n),+0s,2299161j)>, 
#   #<Date: 2001-08-12 ((2452134j,0s,0n),+0s,2299161j)>, 
#   #<Date: 2001-08-14 ((2452136j,0s,0n),+0s,2299161j)> ] 
#  } 
: Bu şu enfes karma doğuracak Ruby

mapping: 
    # Use a sequence as a key 
    ? - foo 
    - bar 
    : 1 

    # Use a mapping as a key 
    ? baz: qux 
    : 2 

    # You can skip the value, which implies `null` 
    ? quux 

    # You can leave the key blank, which implies a `null` key 
    ? 
    : 3 

    # You can even skip both the key and value, so both will be `null` 
    ? 

    # Or you can use a preposterously long scalar as a key 
    ? | 
    We the People of the United States, in Order to form a more 
    perfect Union, establish Justice, insure domestic Tranquility, 
    provide for the common defence, promote the general Welfare, 
    and secure the Blessings of Liberty to ourselves and our 
    Posterity, do ordain and establish this Constitution for the 
    United States of America. 
    : 3 

    # Or just be ridiculous 
    ? - foo: bar 
     baz: 
     - { qux: quux } 
    - stahp 
    : 4 

: Yakut ayrıştırır zaman

{ "mapping" => 
    { [ "foo", "bar" ] => 1, 
    { "baz" => "qux" } => 2, 
    "quux"    => nil, 
    nil    => nil, 
    "We the People of the United States, in Order to form a more\nperfect Union, establish Justice, insure domestic Tranquility,\nprovide for the common defence, promote the general Welfare,\nand secure the Blessings of Liberty to ourselves and our\nPosterity, do ordain and establish this Constitution for the\nUnited States of America.\n" => 3 
    [ { "foo" => "bar", "baz" => [ { "qux" => "quux" } ] }, "stahp" ] => 4 
    } 
} 

Oh, ve "Detroit Tigers" örnek şuna benzer

İlgili konular