Parameters क्या हैं
Parameters वे blanks हैं जो एक method आपको call करते वक्त भरने के लिए कहता है, एक ice cream order में size और flavor की तरह। आपकी choices method क्या करता है यह shape करती हैं।
In this page:
Syntax
def method_name(param1, param2)
# body
end
method_name(arg1, arg2)
Parameters
Method parameters definition के parentheses में listed होते हैं और method body के अंदर available local variables बन जाते हैं। Arguments default से positionally match होते हैं, call site पर pass होने के order में।
अगर arguments की संख्या method की उम्मीद से match न हो Ruby एक ArgumentError raise करता है (जब तक defaults या splat parameters इस्तेमाल न हों)।
Note:
अगर आप required arguments की गलत संख्या के साथ एक method call करें Ruby तुरंत ArgumentError raise करता है।
उदाहरण: Parameters
def greet(name, times)
times.times { puts "Hello, #{name}!" }
end
greet("Sam", 2)
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. #}
आम गलतियां
- arguments की गलत संख्या pass करना, जो
ArgumentError (wrong number of arguments)raise करता है। - arguments को गलत order में pass करना, जैसे
greet(2, "Sam")। - यह उम्मीद करना कि एक parameter reassign करके method caller का variable बदल देगा, जब एक parameter reassign करना सिर्फ local copy बदलता है।
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: