简体   繁体   English

无法访问我的Ruby函数外部定义的数组

[英]Can't access array defined outside my ruby function

How can I access an array defined outside the function? 如何访问函数外部定义的数组? I've tried adding $coins.length as I had read somewhere for global variable — it didn't work. 我尝试添加$coins.length因为我在某处读取了全局变量-它不起作用。 The reason I define the array outside is because there are other functions below that will be pushing a new item into that same array. 我之所以在外面定义数组,是因为下面还有其他函数会将新项目推入同一数组。

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 is an object oriented language. Ruby是一种面向对象的语言。 Even more, Ruby appeared on the scene with a motto “everything is an object.” Even numbers and (sic!) nil in Ruby are objects: 甚至,Ruby的座右铭是“一切都是对象”。Ruby中的偶数和(sic!) nil都是对象:

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

So, you are supposed to use objects for anything that is slightly more complicated than a one-liner. 因此,应该将对象用于比单线稍复杂的任何对象。 Objects have a lot of goodness out of the box: instance variables, lifecycle, etc. 开箱即用的对象有很多优点:实例变量,生命周期等。

class Game
  def initialize
    @coins = []
  end

  def add_coin(value)
    @coins << value
  end

  def coins
    @coins
  end

  def amount
    @coins.size
  end
end

Now you might create in instance of this class and while it's alive, it will hold the value of @coins : 现在,您可以在此类的实例中创建它,并且当它处于活动状态时,它将保留@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

instead of defining global variable, try to define a method which you can use everywhere, like: 与其定义全局变量,不如定义一个可以在任何地方使用的方法,例如:

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

This way, the method will not be global and other methods can also access this method( coins ) within the file. 这样,该方法将不会是全局方法,其他方法也可以在文件内访问此方法( coins )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM