f.seek(0)这个脚本的目的是什么?rewind(current_file)如果文件已被程序打开,为什么我们需要?
input_file = ARGV[0]
def print_all(f)
puts f.read()
end
def rewind(f)
f.seek(0)
end
def print_a_line(line_count,f)
puts "#{line_count} #{f.readline()}"
end
current_file = File.open(input_file)
puts "First Let's print the whole file:"
puts # a blank line
print_all(current_file)
puts "Now Let's rewind, kind of like a tape"
rewind(current_file)
puts "Let's print the first line:"
current_line = 1
print_a_line(current_line, current_file)
Run Code Online (Sandbox Code Playgroud) 是什么args意思,它之间有什么不同以及ARGV是否有区别?
def puts_two(*args)
arg1, arg2 = args
puts "arg1: #{arg1}, arg2: #{arg2}"
end
Run Code Online (Sandbox Code Playgroud) 什么是is_a红宝石又是什么意思整数?
def prime(n)
puts "That's not an integer." unless n.is_a? Integer
is_prime = true
for i in 2..n-1
if n % i == 0
is_prime = false
end
end
if is_prime
puts "#{n} is prime!"
else
puts "#{n} is not prime."
end
end
prime(2)
prime(9)
prime(11)
prime(51)
prime(97)
Run Code Online (Sandbox Code Playgroud)