【问题标题】:Creating an array in Ruby在 Ruby 中创建一个数组
【发布时间】:2020-10-08 00:08:21
【问题描述】:

我正在尝试在 Ruby 中创建一个数组。当调用我的函数来打印数组时,数组似乎是未定义的。我想知道我的语法是否关闭。

有人可以告诉我这是否是在一行中创建和实例化数组的正确方法,还是问题出在我的代码中的其他地方?

下面是我的 Ruby 代码:

    class Game         

        words = Array["hat", "cat", "ate", "run", "eye", "soup", "date",
              "bake", "wake", "grape", "apple", "pride", "drive",
               "tacos", "linux", "orange", "purple", "volume", 
               "liquid", "palace", "molasses", "diamond", "sausage",
               "america", "england"]    


         # starts the game state to play
         def start_x
             #  game logic for begin
             puts(words)
         end

        # if users wins
        def win
             puts("congratulations! you win!")

        end

        # if user loses 
        def death
            puts("sorry! you die!")

         end



   end

【问题讨论】:

  • “我想知道我的语法是否关闭。” – 由于你的方法被调用,你的语法显然是好的,因为要调用你的方法,代码必须实际运行,为了运行,代码必须被解析,为了被解析,它有避免语法错误。如果您没有收到SyntaxError 异常,那么您的语法是可以的。如果要检查语法,可以使用ruby 命令的-c 选项来检查语法。
  • @Jacked_Nerd:虽然您的数组创建显然是正确的,但如果您是一个懒惰的打字员,更简单的方法是words=%w(cat ate run .... england)。但是,您不能从方法内部访问变量 words。例如,您可以将其设为与类关联的变量,即@@words=%w(....),然后当然将其输出为puts @@words

标签: arrays ruby variables syntax


【解决方案1】:

您要么需要将words 常量化为WORDS,要么需要通过getter 方法或实例变量使这些值可用。以下是一些示例:

常量化,使数组可供任何调用者使用:

class Game
  WORDS = ['hat', 'cat', 'ate', 'run', 'eye', 'soup', 'date',
    'bake', 'wake', 'grape', 'apple', 'pride', 'drive',
    'tacos', 'linux', 'orange', 'purple', 'volume', 
    'liquid', 'palace', 'molasses', 'diamond', 'sausage',
    'america', 'england']

  def start_x
    puts(WORDS)
  end
end

然后它就起作用了:

⇒ Game.new.start_x
hat
cat
ate
run
eye
soup
date
bake
wake
grape
apple
pride
drive
tacos
linux
orange
purple
volume
liquid
palace
molasses
diamond
sausage
america
england

或者使用getter方法:

class Game
  def words
    @words ||= ['hat', 'cat', 'ate', 'run', 'eye', 'soup', 'date',
      'bake', 'wake', 'grape', 'apple', 'pride', 'drive',
      'tacos', 'linux', 'orange', 'purple', 'volume', 
      'liquid', 'palace', 'molasses', 'diamond', 'sausage',
      'america', 'england']
  end

  def start_x
    puts(words)
  end
end

或者使用实例变量:

class Game
  def initialize
    @words = ['hat', 'cat', 'ate', 'run', 'eye', 'soup', 'date',
      'bake', 'wake', 'grape', 'apple', 'pride', 'drive',
      'tacos', 'linux', 'orange', 'purple', 'volume', 
      'liquid', 'palace', 'molasses', 'diamond', 'sausage',
      'america', 'england']
  end

  def start_x
    puts(@words)
  end
end

或者结合属性阅读器:

class Game
  attr_reader :words

  def initialize
    @words = ['hat', 'cat', 'ate', 'run', 'eye', 'soup', 'date',
      'bake', 'wake', 'grape', 'apple', 'pride', 'drive',
      'tacos', 'linux', 'orange', 'purple', 'volume', 
      'liquid', 'palace', 'molasses', 'diamond', 'sausage',
      'america', 'england']
  end

  def start_x
    puts(words)
  end
end

所有的工作方式都相同,将在不同的情况下使用。

【讨论】:

    【解决方案2】:

    您的代码不起作用,因为words 是在方法之外声明的局部变量

    您可能希望在此处有一个实例变量。将游戏代码与数据分开通常是个好主意。因此,不是将单词硬编码到 Game 类中,而是在初始化时传递数据:

    class Game
      def initialize(words)
        @words = words
      end
    
      def start_x
        puts @words
      end
    
      # ...
    end
    

    称呼它:

    words = %w[
      hat cat ate run eye soup date bake wake grape apple pride drive tacos linux
      orange purple volume liquid palace molasses diamond sausage america england
    ]
    
    game = Game.new(words)
    game.start_x
    

    从这里开始,您可以轻松地将数据提取到words.txt 文件中:

    hat
    cat
    ate
    ...
    sausage
    america
    england
    

    并通过以下方式加载数据:

    words = File.readlines('words.txt' chomp: true)
    
    game = Game.new(words)
    game.start_x
    

    这使您无需修改​​代码即可使用不同的词组启动游戏。

    【讨论】:

      【解决方案3】:

      我想这会对你有所帮助。

      class Game
        attr_reader :words
        def initialize
          @words = %w[hat cat ate run eye soup date
                      bake wake grape apple pride drive
                      tacos linux orange purple volume
                      liquid palace molasses diamond sausage
                      america england]
        end
        # if users wins
        def win
          puts("congratulations! you win!")
        end
        # if user loses 
        def death
          puts("sorry! you die!")
        end
      end
      

      你可以访问像Game.new.words这样的词

      【讨论】:

        猜你喜欢
        • 2012-08-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-26
        相关资源
        最近更新 更多