2015-11-29 8 views
8

YAML specs पर एक पैराग्राफ 2.11 प्रश्न चिह्न के बारे में नहीं है:वाईएएमएल में एक जटिल मैपिंग कुंजी क्या है?

प्रश्न चिह्न और अंतरिक्ष ("?") एक जटिल मानचित्रण कुंजी संकेत मिलता है। ब्लॉक संग्रह के भीतर, कुंजी: वैल्यू जोड़े डैश, कोलन या प्रश्न चिह्न के तुरंत बाद शुरू हो सकते हैं।

यह उदाहरण दिया जाता है:

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

यह अन्य उदाहरण भी असफल एक्सएमएल के लिए परिवर्तित किया:

%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", 
     ] 
    } 
} 

दुर्भाग्य से मुझे समझ नहीं आता क्या करता है इसका मतलब है। मैंने इसे codebeautify का उपयोग करके एक्सएमएल में बदलने की कोशिश की, लेकिन मुझे एक त्रुटि मिली।

तो मेरा प्रश्न है:

प्रश्न चिह्न क्या करता है?

उत्तर

23

spec बहुत स्पष्ट नहीं है, लेकिन कुछ सिर-स्क्रैचिंग के बाद मैंने इसे समझ लिया। वाईएएमएल में दो प्रकार की ब्लॉक मैपिंग कुंजी हैं: निहित और स्पष्ट।

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

हम रूबी में इस YAML लोड (उदाहरण के लिए) हम निम्न परिणाम हो:

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

लेकिन हम भी उपयोग कर सकते हैं अंतर्निहित शैली तरह से परिचित हैं है स्पष्ट शैली एक ही बात को व्यक्त करने के:

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

दूसरे शब्दों में, एक लाइन ? के साथ शुरू एक महत्वपूर्ण संकेत करता है और एक लाइन 01,236 के साथ शुरूएक मूल्य इंगित करता है।

इसका क्या उपयोग है? खैर, यह हमें किसी भी वाईएएमएल संरचना को मानचित्रण कुंजी के रूप में उपयोग करने देता है। एक मैपिंग कुंजी के रूप में अनुक्रम का उपयोग करना चाहते हैं? आप ऐसा कर सकते हैं! मैपिंग कुंजी के रूप में मैपिंग का उपयोग करना चाहते हैं? निहारना:

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 

रूबी में यह निम्नलिखित रमणीय हैश प्राप्त होते हैं:

{ "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 
    } 
} 

ओह, और "डेट्रॉइट टाइगर्स" उदाहरण इस तरह दिखता है जब रूबी यह पार्स करता है:

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)> ] 
#  } 
संबंधित मुद्दे