Proc.new क्या है
एक Proc एक block को एक real object में wrap करता है जिसे आप एक variable में save कर सकते हैं और जब चाहें चला सकते हैं। यह instructions को एक card पर record करने जैसा है जिसे आप रख और reuse कर सकते हैं।
In this page:
Syntax
proc_name = Proc.new { |param| expression }
proc_name.call(argument)
Proc.new
एक Proc code के एक block को एक reusable, storable object की तरह wrap करता है जिसे एक variable को assign किया जा सकता है और बाद में .call से call किया जा सकता है।
Proc.new { ... } (या shorthand proc { ... }) explicitly एक बनाता है, एक plain block के उलट जो सिर्फ एक single call site पर inline exist करता है। Procs arguments की संख्या के बारे में lenient हैं -- बहुत ज़्यादा या बहुत कम pass करना एक error raise नहीं करता।
Note:
एक Proc silently extra arguments ignore करता है और missing वालों को nil से भरता है, एक Lambda के उलट जो arity strictly enforce करता है।
उदाहरण: Proc.new
square = Proc.new { |x| x * x }
puts square.call(5)
puts square.(6)
puts square[7]
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. #}
आम गलतियां
- एक Proc को गलत संख्या arguments के साथ call करना और एक error की उम्मीद करना, जब extra arguments ignore होते हैं और missing वाले
nilबन जाते हैं। - एक Proc के अंदर
returnइस्तेमाल करना, जो enclosing method से return करता है और top level परLocalJumpErrorraise कर सकता है। .call,.(), या[]भूल जाना, इसलिए Proc कभी नहीं चलता।
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: