Symbol Keys का इस्तेमाल
Symbol keys एक hash में labels की तरह :name जैसे names इस्तेमाल करती हैं, जो usual Ruby habit है। यह drawer labels की तरह short, reusable tags इस्तेमाल करने जैसा है।
In this page:
Syntax
hash = { name: value1, other_name: value2 }
hash[:name]
Symbol Keys
Hash keys की तरह symbols (:name) इस्तेमाल करना idiomatic Ruby convention है, क्योंकि symbols immutable हैं और strings से तेज़ compare होते हैं।
Shorthand { name: "Alice" } syntax specifically symbol keys के लिए है और { :name => "Alice" } के बराबर है। same hash में symbol और string keys mix करना legal है लेकिन usually inconsistent design का एक sign है।
Note:
{ name: "Alice" } और { :name => "Alice" } exactly equivalent हैं -- colon-shorthand बस newer, ज़्यादा concise syntax है।उदाहरण: Symbol Keys
options = { color: "red", size: "large" }
same_thing = { :color => "red", :size => "large" }
puts options == same_thing
puts options[:color]
Login to try C/C++/Java/PHP code in the editor
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
आम गलतियां
"color"और:colorको keys की तरह mix करना, इसलिएoptions["color"]nilreturn करता है।- बिना colon के
{ color => "red" }लिखना, जोcolorनाम का एक variable ढूंढता है। - short form में spaces या dashes वाली keys इस्तेमाल करना जैसे
{ first-name: 1 }, जो एक syntax error है;"first-name": 1या:"first-name"इस्तेमाल करें।
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: