-1

我正在尝试编写一个简单的命令行程序,它可以让我记录在学习过程中分心的次数。

当我运行此代码说我的file.open参数无效时,我收到一个参数错误。如果我想new_session用时间戳命名每个文件,那么简单的解决方案是什么?

def first_question
    puts "Would you like to start a new session?"
    answer = gets.chomp
    answer = answer.downcase

    if answer == "yes"
        new_session
    else
        puts "Ok. Would you like to review a previous session?"
        prev_session = gets.chomp
        prev_session.downcase
        if prev_session == "yes"
            #GET AND REVIEW PREVIOUS SESSIONS
        elsif prev_session == "no"
            puts "Well if you don't want a new session, and you don't want to review your old sessions, then you're SOL."
        else
            "That's not an acceptable response."
            first_question
        end
    end
end

def new_session
    distractions = 0
    d = File.open("Session"+Time.now.to_s , 'w'){|f| f.write(distractions) }


    puts "What would you like to do (add track(s) or review tracks)?"
    request = gets.chomp
    request.downcase    
    if request == "add track"
        distractions = distractions.to_i + 1
        puts "You have #{distractions} tracks in this session."
    elsif request == "add tracks"
        puts "How many times have you been distracted since we last met?"
        answer = gets.chomp
        distractions = distractions.to_i + answer.to_i
        puts "You have #{distractions} tracks."
    elsif request == "review tracks"
        puts distractions
    end
    File.open( d , 'w') {|f| f.write(distractions) }
end

first_question
4

2 回答 2

1

你的大部分代码都是混乱和冗余的。但是,您所指的问题来自这里:

d = File.open("Session"+Time.now.to_s , 'w'){|f| f.write(distractions) }

d将是写入文件的字节数,因此是 Fixnum。您无法在函数的最后一行打开您尝试执行的 Fixnum。

更远,

request = gets.chomp
request.downcase

这里的第二行什么都不做。

于 2013-11-02T22:55:31.467 回答
0

你有两个File.open陈述:

d = File.open("Session"+Time.now.to_s , 'w'){|f| f.write(distractions) }

和:

File.open( d , 'w') {|f| f.write(distractions) }

你的错误代码会告诉你哪一个是错的,但是,从他们的角度来看,我会说这是第二个。

d将分配第一个块的结果File.open,这将是f.write(distractions)

File.open文档说:

块的值将从 File.open 返回。

File.write文档说:

返回写入的字节数。

结果,您分配d了多个字节,然后尝试使用整数作为文件名创建文件,这是错误的,因为文件名必须是字符串。

这导致了一个更大的问题,即您的代码毫无意义。

  • d = File.open("Session"+Time.now.to_s , 'w'){|f| f.write(distractions) }将 a 写入由 .0创建的文件"Session"+Time.now.to_s
  • request.downcase将 的内容转换request为小写并立即将其丢弃。也许你的意思是request.downcase!,但最好写:

    request = gets.chomp
    request.downcase    
    

    作为:

    request = gets.chomp.downcase
    
  • distractions = distractions.to_i + 1? distractions已经0是一个 Fixnum。您正在将 Fixnum 转换为整数,to_i然后将其加 1。只需这样做:

    distractions += 1
    
  • distractions = distractions.to_i + answer.to_i应该:

    distractions += answer.to_i
    
  • File.open( d , 'w') {|f| f.write(distractions) },因为它正在尝试写入具有整数名称的文件,因此不会更新您的原始文件。如果成功,它将写入一个全新的文件,最终将覆盖先前创建的文件,这是将单个文件写入0磁盘的结果。反而,d应该是先前创建的文件的名称。

考虑一下:

def new_session

  distractions = 0

  puts "What would you like to do (add track(s) or review tracks)?"
  request = gets.chomp.downcase

  case request
  when "add track"
    distractions += 1
    puts "You have #{distractions} tracks in this session."

  when "add tracks"
    puts "How many times have you been distracted since we last met?"
    distractions += gets.chomp.to_i
    puts "You have #{distractions} tracks."

  when "review tracks"
    puts distractions
  end

  File.write( "Session" + Time.now.to_s, distractions) 
end

这段代码更干净,现在更有意义。

于 2013-11-02T22:54:53.143 回答