← Back to Ruby Course | Chapter 11: File I/O & Exceptions | Lesson 6 of 6

ARGV क्या है

ARGV आपकी script का नाम चलाते वक्त इसके बाद type किए गए extra words की list है, order करते वक्त आपके बोले गए toppings की तरह। आपका program इन्हें पढ़ और इनके हिसाब से act कर सकता है।

In this page:

  1. ARGV
Syntax
ruby
# 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

markup
# 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"
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. कोई arguments न दिए जाने पर ARGV[0] पढ़ना, जो nil है और फिर टूट जाता है।
  2. यह उम्मीद करना कि ARGV में script name शामिल होगा, जब यह सिर्फ arguments hold करता है।
  3. यह भूल जाना कि सारी ARGV values strings हैं, इसलिए ARGV[0] + 1 तब तक fail होता है जब तक आप .to_i इस्तेमाल न करें।
चैप्टर सारांश
  • File.read, File.write, और block के साथ File.open files पढ़ते और लिखते हैं।
  • begin, rescue, ensure, और raise errors handle और signal करते हैं।
  • Custom exceptions और ARGV error types और command-line input support करते हैं।
🔒

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.