1
在下面的腳本:命令行參數和`gets`
first, second, third = ARGV
puts "The oldest brothers name is #{first}"
puts "The middle brothers name is #{second}"
puts "The youngest brothers name is #{third}"
puts "What is your moms name?"
mom = $stdin.gets.chomp
puts "What is your dads name?"
dad = $stdin.gets.chomp
puts "In the my family there are three sons #{first}, #{second}, #{third}, and a mom named #{mom}, and a father named #{dad}"
使用gets
命令沒有$stdin
我不能接受用戶輸入。我必須使用$stdin.gets
才能正常工作。
這是爲什麼? ARGV
是做什麼來禁用它的?默認情況下$stdin
與gets
命令不包含在內?
無法複製。只要您在終端調用腳本時不傳遞任何參數,它就會在代碼中引用'ARGV'並且不使用'$ stdin'。 – sawa
它看起來好像是用3個參數調用腳本,列出3個兄弟的名字。如果這些名稱恰好是文件,那麼'gets'將會讀取它們,否則不會。 – user12341234