Zed Shaw の Learn Ruby the Hard Way の演習 21:
def add(a, b)
puts "ADDING #{a} + #{b}"
a + b
end
age = add(30, 5)
puts "Age: #{age}"
これは Age: 35 を出力します。
私は前の演習(例20)でこれをやってみました:
def print_all(f)
puts f.read()
end
current_file = File.open(input_file)
sausage = print_all(current_file)
puts "Sausage: #{sausage}"
しかし、実行すると、ファイル ポインタを 0 に戻した後でも #{sausage} が出力されません。
def print_all(f)
puts f.read()
end
def rewind(f)
f.seek(0, IO::SEEK_SET)
end
current_file = File.open(input_file)
sausage = print_all(current_file)
rewind(current_file)
puts "Sausage: #{sausage}"
メソッド add(a, b) からの戻り値を age に割り当てましたが、なぜ print_all(current_file) で同じことができないのでしょうか?