ARGV क्या है
ARGV आपकी script का नाम चलाते वक्त इसके बाद type किए गए extra words की list है, order करते वक्त आपके बोले गए toppings की तरह। आपका program इन्हें पढ़ और इनके हिसाब से act कर सकता है।
In this page:
Syntax
# ruby script.rb arg1 arg2
first = ARGV[0]
ARGV.each { |arg| puts arg }
ARGV
ARGV एक global array है जो एक Ruby script के चलते वक्त इसे pass किए command-line arguments hold करता है, script name को छोड़कर।
ARGV[0] पहला argument है, ARGV.length count देता है, वगैरह -- यह बिल्कुल एक normal array जैसा behave करता है। यह standard तरीका है जिससे एक Ruby script इसे command line से pass किया input पढ़ता है।
Note:
ARGV हमेशा strings रखता है, भले ही एक argument numeric दिखे -- अगर आपको एक number चाहिए .to_i या .to_f से convert करें।
उदाहरण: ARGV
# Simulating ARGV since this runs without real command-line arguments:
simulated_argv = ["report.txt", "--verbose"]
puts "Argument count: #{simulated_argv.length}"
puts "First argument: #{simulated_argv[0]}"
puts "In a real script, this data comes from the built-in ARGV array"
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 न दिए जाने पर
ARGV[0]पढ़ना, जोnilहै और फिर टूट जाता है। - यह उम्मीद करना कि
ARGVमें script name शामिल होगा, जब यह सिर्फ arguments hold करता है। - यह भूल जाना कि सारी
ARGVvalues strings हैं, इसलिएARGV[0] + 1तब तक fail होता है जब तक आप.to_iइस्तेमाल न करें।
चैप्टर सारांश
File.read,File.write, और block के साथFile.openfiles पढ़ते और लिखते हैं।begin,rescue,ensure, औरraiseerrors handle और signal करते हैं।- Custom exceptions और
ARGVerror types और command-line input support करते हैं।
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: