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

Lambda क्या है

एक lambda एक careful तरह का Proc है जो check करता है आपने कितने inputs दिए, एक strict form की तरह जो पूरी तरह भरी होनी चाहिए। यह arrow shortcut या word lambda से लिखा जाता है।

In this page:

  1. Lambda
Syntax
ruby
lambda_name = lambda { |param| expression }
lambda_name = ->(param) { expression }
lambda_name.call(argument)

Lambda

एक lambda (lambda { ... } या -> "stabby lambda" syntax से बनाया गया) एक Proc जैसा है लेकिन एक regular method जैसा ज़्यादा behave करता है: यह arguments की exact संख्या enforce करता है, और इसके अंदर return सिर्फ lambda से खुद exit करता है, enclosing method से नहीं।

Lambdas आमतौर पर Procs से prefer किए जाते हैं जब आपको method-like strictness और behavior चाहिए। Proc और Lambda दोनों under the hood same Proc class के instances हैं।

Note: ->(x) { x * x } stabby lambda syntax lambda { |x| x * x } की तुलना में एक lambda लिखने का ज़्यादा modern, concise तरीका है।

उदाहरण: Lambda

markup
square = lambda { |x| x * x }
cube = ->(x) { x * x * x }

puts square.call(4)
puts cube.call(3)
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. lambda इस्तेमाल करना और गलत संख्या arguments के साथ call करना, जो ArgumentError raise करता है।
  2. lambda { |x| x * x } लिखना और इसे square(4) की तरह call करना, जो NoMethodError raise करता है; एक lambda को square.call(4), square.(4), या square[4] से call होना चाहिए।
  3. .call भूल जाना और इसके result की बजाय lambda object print करना।
🔒

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.