新手问题 根据笨方法学 ruby 碰到奇怪问题?求解

bluetea · 2013年06月16日 · 最后由 chinakr 回复于 2013年06月18日 · 4463 次阅读

http://lrthw.github.io/ex16/ 根据这个教程的第 16 章敲的代码:确保和教程一样, filename = ARGV.first script = $0

puts "We're going to erase #{filename}." puts "If you don't want that, hit CTRL-C (^C)." puts "If you do want that, hit RETURN."

print "? " STDIN.gets

puts "Opening the file..." target = File.open(filename, 'w')

puts "Truncating the file. Goodbye!" target.truncate(target.size) #这个就是 15 行

puts "Now I'm going to ask you for three lines."

print "line 1: "; line1 = STDIN.gets.chomp() print "line 2: "; line2 = STDIN.gets.chomp() print "line 3: "; line3 = STDIN.gets.chomp()

puts "I'm going to write these to the file."

target.write(line1) target.write("\n") target.write(line2) target.write("\n") target.write(line3) target.write("\n")

puts "And finally, we close it." target.close()

有个奇怪的问题: 输出如下: C:\ruby\site>ruby test.rb bbb.txt We're going to erase bbb.txt. If you don't want that, hit CTRL-C (^C). If you do want that, hit RETURN. ? Opening the file... Truncating the file. Goodbye! test.rb:15: undefined method `size' for #File:bbb.txt (NoMethodError)

报错说第 15 行没有没 size 定义 method,请问是我电脑的环境问题吗?求解? 另外问下,代码的第二行,定义那个的作用是什么?

因为你用的 ruby1.8,版本应该和教材一致

好的,多谢,我晚上有 mac 试下!

这类教程,例子的可读性、启发性和趣味性很重要

需要 登录 后方可回复, 如果你还没有账号请 注册新账号