← Back to Ruby Course | Chapter 8: Blocks, Procs & Lambdas | Lesson 3 of 6

Proc.new क्या है

एक Proc एक block को एक real object में wrap करता है जिसे आप एक variable में save कर सकते हैं और जब चाहें चला सकते हैं। यह instructions को एक card पर record करने जैसा है जिसे आप रख और reuse कर सकते हैं।

In this page:

  1. Proc.new
Syntax
ruby
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

markup
square = Proc.new { |x| x * x }

puts square.call(5)
puts square.(6)
puts square[7]
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. #}
आम गलतियां
  1. एक Proc को गलत संख्या arguments के साथ call करना और एक error की उम्मीद करना, जब extra arguments ignore होते हैं और missing वाले nil बन जाते हैं।
  2. एक Proc के अंदर return इस्तेमाल करना, जो enclosing method से return करता है और top level पर LocalJumpError raise कर सकता है।
  3. .call, .(), या [] भूल जाना, इसलिए Proc कभी नहीं चलता।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.