通常,变量的形式应为snake_case
和 中的常量SCREAMING_SNAKE_CASE
。很难说你是想创建常量还是变量。
您还可以使用更简单的方式实例化您的数组%w{elements here}
。
浏览文档以了解这里发生了什么。
faces = %w{Two Three Four Five Six Seven Eight Nine Ten Jack Queen King Ace}
=> ["Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace"]
suites = %w{Spades Clubs Hearts Diamonds}
=> ["Spades", "Clubs", "Hearts", "Diamonds"]
faces.product(suites)
=> [["Two", "Spades"], ["Two", "Clubs"], ["Two", "Hearts"], ["Two", "Diamonds"], ["Three", "Spades"], ["Three", "Clubs"], ["Three", "Hearts"], ["Three", "Diamonds"], ["Four", "Spades"], ["Four", "Clubs"], ["Four", "Hearts"], ["Four", "Diamonds"], ["Five", "Spades"], ["Five", "Clubs"], ["Five", "Hearts"], ["Five", "Diamonds"], ["Six", "Spades"], ["Six", "Clubs"], ["Six", "Hearts"], ["Six", "Diamonds"], ["Seven", "Spades"], ["Seven", "Clubs"], ["Seven", "Hearts"], ["Seven", "Diamonds"], ["Eight", "Spades"], ["Eight", "Clubs"], ["Eight", "Hearts"], ["Eight", "Diamonds"], ["Nine", "Spades"], ["Nine", "Clubs"], ["Nine", "Hearts"], ["Nine", "Diamonds"], ["Ten", "Spades"], ["Ten", "Clubs"], ["Ten", "Hearts"], ["Ten", "Diamonds"], ["Jack", "Spades"], ["Jack", "Clubs"], ["Jack", "Hearts"], ["Jack", "Diamonds"], ["Queen", "Spades"], ["Queen", "Clubs"], ["Queen", "Hearts"], ["Queen", "Diamonds"], ["King", "Spades"], ["King", "Clubs"], ["King", "Hearts"], ["King", "Diamonds"], ["Ace", "Spades"], ["Ace", "Clubs"], ["Ace", "Hearts"], ["Ace", "Diamonds"]]
请记住,产品仅在 Ruby 1.9 >= 中可用。