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