【问题标题】:Why will this not iterate through the file like I want it to?为什么这不会像我想要的那样遍历文件?
【发布时间】:2019-10-18 11:27:39
【问题描述】:

当它应该打印卡ID和余额时,它只会在屏幕上输出两个空白行

我已经完全重写了代码。 我已经摆弄了那个代码一个小时

class RBC

    def initialize

        @args = ["Create a new card"]
        @functions = ["create_rbc"]

        puts "Do you have an RBC ID yet? Yes(0) No(1)"

        hasrbc = gets.chomp.to_i

        if hasrbc == 1 

            @balance = 5
            create_rbc

        else

            login

        end

    end

    def create_rbc

    puts "\nGenerating your rbc\n\n"
        puts "\nWelcome to your Ruby Binary Card(RBC)!\n\n"
        puts "Your RBC will keep track of your RubyCredits(RC).\n"
        puts "You will get paid RC for work apps, and pay for game apps.\n"
        puts "If you lose track of your RBC ID, you can get a new one.\n"
        puts "Doing this, however, will reset your balance to the default of $5\n\n"
        puts "What is your name? Do first last\n"

        @fullname = gets.chomp
        @card_name = get_name_codec(@fullname)
        @card_cipher = "#{rand(1..9)}#{rand(1..9)}#{rand(1..9)}#{rand(1..9)}#{rand(1..9)}#{rand(1..9)}#{rand(1..9)}#{rand(1..9)}#{rand(1..9)}#{rand(1..9)}"

        @card_id = "#{@card_name} - #{@card_cipher}"
        instance_variable_set("@Id#{@card_cipher}", @balance)

        puts "Write down your RBC ID: #{@card_id}"
        file = File.open("Cards.rbc", "w")
            file.puts @card_id
            file.puts @balance
        end

    end

    def get_name_codec(name)

        names = name.split(" ")

        fname = names[0]
        lname = names[1]

        fchar = fname.split(//)
        fcodec = "#{fchar[0]}#{fchar[1]}"

        name_codec = "#{fcodec}#{lname}"

        return name_codec

    end

    def login
        @found = false
        puts "What is your RBC Id"
        input = gets.chomp
        File.open("Cards.RBC", "r") do |f|
            f.each_line do |line|

                if input == "#{line}"
                    @card_id = line.to_s
                    @found == true
                elsif @found == true
                    @balance = line.to_i
                end


            end

        end

        puts "#{@card_id}#{@balance}"
end

RBC.new

然后是 Cards.RBC


TiLan - 1122632527
5

我希望它打印余额和卡 ID。 它应该给我我的卡 ID,然后是这样的余额: 0000...等 5

【问题讨论】:

    标签: ruby class oop object


    【解决方案1】:

    input = gets.chomp 会从输入中删除换行符,但f.each_line 不会。所以input == "#{line}" 正在比较,例如,"1234""1234\n"

    也切线。

        input = gets.chomp
        File.open("Cards.RBC", "r") do |f|
            f.each_line do |line|
                line.chomp!
                if input == line
                    @card_id = line
                    @found == true
                elsif @found == true
                    @balance = line.to_i
                end
            end
        end
    

    您可以通过使用p 打印值来调试此类事情。这会将它们显示为带引号的字符串并显示任何特殊字符,包括换行符。

    p line
    p input
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多