Frozen String Literals क्या हैं
बिल्कुल top पर एक special comment file में हर text को locked बना सकता है ताकि इसे बदला न जा सके। यह आपके सारे notes laminate करने जैसा है ताकि कोई इन पर scribble न कर सके।
In this page:
Syntax
# frozen_string_literal: true
string = "literal" # frozen, cannot be modified
Frozen String Literals
एक Ruby file की बिल्कुल पहली line की तरह # frozen_string_literal: true add करना उस file में हर string literal को default से frozen (immutable) बनाता है, जो unnecessary string object allocation avoid करके performance improve कर सकता है।
यह अब Ruby projects और gems में एक common convention है। Individual strings एक mutable copy पाने के लिए .dup call करके अभी भी explicitly unfrozen हो सकती हैं।
Note:
अगर आपको specifically इसकी एक mutable copy चाहिए एक frozen string पर .dup call करें।
उदाहरण: Frozen String Literals
# frozen_string_literal: true
name = "Alice"
puts name.frozen?
mutable_copy = name.dup
puts mutable_copy.frozen?
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. #}
आम गलतियां
# frozen_string_literal: truecomment को दूसरी lines के बाद रखना, जब इसे काम करने के लिए top पर होना ज़रूरी है।- बाद में
<<से एक string modify करना, जोFrozenErrorraise करता है;dupया+""इस्तेमाल करें। - यह मान लेना कि यह हर case में interpolated strings freeze करता है, जब यह सिर्फ बिना interpolation वाले literals पर apply होता है।
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: