Lambda क्या है
एक lambda एक careful तरह का Proc है जो check करता है आपने कितने inputs दिए, एक strict form की तरह जो पूरी तरह भरी होनी चाहिए। यह arrow shortcut या word lambda से लिखा जाता है।
In this page:
Syntax
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
square = lambda { |x| x * x }
cube = ->(x) { x * x * x }
puts square.call(4)
puts cube.call(3)
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. #}
आम गलतियां
lambdaइस्तेमाल करना और गलत संख्या arguments के साथ call करना, जोArgumentErrorraise करता है।lambda { |x| x * x }लिखना और इसेsquare(4)की तरह call करना, जोNoMethodErrorraise करता है; एक lambda कोsquare.call(4),square.(4), याsquare[4]से call होना चाहिए।.callभूल जाना और इसके result की बजाय lambda object print करना।
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: