【问题标题】:Can't access array defined outside my ruby function无法访问在我的 ruby​​ 函数之外定义的数组
【发布时间】:2017-07-28 22:21:50
【问题描述】:

如何访问在函数外部定义的数组?我试过添加$coins.length,因为我在某处读过全局变量——它没有用。我在外面定义数组的原因是因为下面还有其他函数会将新项目推送到同一个数组中。

coins = []

def start

    puts "It's a sunny day. It's warm and you feel great."
    puts "You've picked up your son from creche. He's in a good mood and you've got home no problem."
    puts "This is when troubles start, and you know it."
    puts "You go out of the elevator and before you reach the door... 'give me the keeys'!"
    puts "Do you give him the keys? (Y/N)"
    print "> "


    while keys = gets.chomp.downcase
        case keys
        when "y"
            puts "you are in, good job. And you get a coin"
            coins.push("1")
            puts "you now have #{coins.length} coins"
            room
        when "n"
            cry("I wanted to dooooooo iiiiiiiit!")
        else    
            again
        end
    end
end

【问题讨论】:

    标签: ruby


    【解决方案1】:

    Ruby 是一种面向对象的语言。更重要的是,Ruby 以“一切皆对象”的座右铭出现在现场。 Ruby 中的偶数和(原文如此!)nil 是对象:

    ▶ 42.class
    #⇒ Fixnum < Integer
    ▶ nil.__id__
    #⇒ 8
    

    因此,您应该将对象用于比单行稍微复杂的任何事情。对象有很多开箱即用的优点:实例变量、生命周期等。

    class Game
      def initialize
        @coins = []
      end
    
      def add_coin(value)
        @coins << value
      end
    
      def coins
        @coins
      end
    
      def amount
        @coins.size
      end
    end
    

    现在你可以在这个类的实例中创建,当它还活着时,它会保存@coins的值:

    game = Game.new
    game.add_coin("1")
    puts game.coins
    #⇒ ["1"]
    puts game.amount
    #⇒ 1
    game.add_coin("1")
    game.add_coin("2")
    puts game.coins
    #⇒ ["1", "1", "2"]
    puts game.amount
    #⇒ 3
    

    【讨论】:

    • 可能是我得到的最明确的答案之一,亲爱的!
    【解决方案2】:

    不要定义全局变量,而是尝试定义一个可以在任何地方使用的方法,例如:

    def coins
      @coins ||= []
    end
    
    def start
    
        puts "It's a sunny day. It's warm and you feel great."
        puts "You've picked up your son from creche. He's in a good mood and you've got home no problem."
        puts "This is when troubles start, and you know it."
        puts "You go out of the elevator and before you reach the door... 'give me the keeys'!"
        puts "Do you give him the keys? (Y/N)"
        print "> "
    
    
        while keys = gets.chomp.downcase
            case keys
            when "y"
                puts "you are in, good job. And you get a coin"
                coins.push("1")
                puts "you now have #{coins.length} coins"
                room
            when "n"
                cry("I wanted to dooooooo iiiiiiiit!")
            else    
                again
            end
        end
    end
    

    这样,该方法将不是全局的,其他方法也可以在文件中访问该方法(coins)。

    【讨论】:

    • ||= 的有趣用法。我读过很多书,比如stackoverflow.com/questions/995593/…,但我仍然无法弄清楚它在这种情况下的作用。有趣的是@coins = [] 不起作用。可以详细说明 ||= 在这种情况下正在做什么?谢谢!
    • foo ||= 42foo = foo || 42 的简写,例如` foo += 42` 是foo = foo + 42 的简写。这里用于设置 @coins 实例变量,当且仅此之前没有设置。
    猜你喜欢
    • 1970-01-01
    • 2019-08-27
    • 1970-01-01
    • 2021-05-22
    • 2021-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-16
    相关资源
    最近更新 更多