-2

可能重复:
Ruby 中两个数组的组合

Faces = "Two Three Four Five Six Seven Eight Nine Ten Jack Queen King Ace"
Suits = "Spades Clubs Hearts Diamonds"
Face = []
Face = Faces.split * 4
Suits.split

我正在寻求将suits.split array每件西装放入并输入 13 次Face以创建一个看起来像的 2D 数组[["Two", "Spades"],["Three", "Spades"]... ["Ace", "Diamonds"]]

4

2 回答 2

3

正如 Sunny 链接的问题中所发布的那样,您正在寻找product

Faces.product Suits
于 2013-02-04T19:30:48.267 回答
1

通常,变量的形式应为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 >= 中可用。

于 2013-02-04T19:32:07.607 回答