为什么这个逻辑在初始化方法中不起作用?

时间:2014-03-15 23:36:10

标签: ruby

  def initialize(letters)
    @letters = letters
    @face = letters.sample      # letters is an array of all letters from A to Z
    if @face == "Q"
      @face = "Qu"
    end
    @visited = false
    @coord = []
  end

当我p我的骰子后,我发现@face仍然是“Q”

..., [#<Dice:0x007f907b032948 @letters=["H", "I", "M", "N", "Q", "U"], @face="Q", @visited=false, @coord=[]>, ...

发生了什么事?

1 个答案:

答案 0 :(得分:0)

示例方法从数组中选择一个随机元素。我想你会发现,如果@letters等于[&#39; Q&#39;],你的代码就可以了。