如何在 Ruby 中打乱数组/散列?
为了学习目的,这叫什么?正在创建的对象是数组还是哈希?
stack_of_cards = []
这就是我填充它的方式:
stack_of_cards << Card.new("A", "Spades", 1)
stack_of_cards << Card.new("2", "Spades", 2)
stack_of_cards << Card.new("3", "Spades", 3)
...
这是我的 Card 类:
class Card
attr_accessor :number, :suit, :value
def initialize(number, suit, value)
@number = number
@suit = suit
@value = value
end
def to_s
"#{@number} of #{@suit}"
end
end
我想对这个数组/哈希中的元素进行洗牌(这叫什么?:S)
有什么建议吗?
For learning purposes, what is this called? Is the object being created an array or a hash?
stack_of_cards = []
This is how I'm filling it:
stack_of_cards << Card.new("A", "Spades", 1)
stack_of_cards << Card.new("2", "Spades", 2)
stack_of_cards << Card.new("3", "Spades", 3)
...
Here is my Card class:
class Card
attr_accessor :number, :suit, :value
def initialize(number, suit, value)
@number = number
@suit = suit
@value = value
end
def to_s
"#{@number} of #{@suit}"
end
end
I'd like to shuffle the elements in this array/hash (what is this called? :S)
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
它是一个数组,请参见 http://www.ruby-doc.org/core- 1.8.7/classes/Array.html 了解更多信息。
我编写了功能形式,它返回一个新的数组,并且它是经过洗牌的新数组。您可以改为使用:
...将数组就地打乱。
It is an Array, see http://www.ruby-doc.org/core-1.8.7/classes/Array.html for more information.
I've written the functional form, which returns a new Array, and it's the new one that's shuffled. You can instead use:
...to shuffle the array in-place.
如果你想洗牌哈希,你可以使用这样的东西:
我已经发布了这个答案,因为如果我搜索“ruby shuffle hash”,我总是会找到这个问题。
If you want to shuffle a hash you can use something like this:
I've posted this answer since I always find this question if I search for "ruby shuffle hash".
除了使用shuffle方法,您可以使用 sort 方法:
如果您使用的是未实现
shuffle
的旧版本 Ruby,这可能很有用。与shuffle!
一样,您可以使用sort!
来处理现有数组。In addition to using the shuffle method, you can use the sort method:
This may be of use if you are using an older version of Ruby where
shuffle
is not implemented. As withshuffle!
, you can usesort!
to work on the existing array.如果您想疯狂地编写自己的就地洗牌方法,您可以这样做。
If you wanted to get crazy and write your own in-place shuffle method, you could do something like this.
对于数组:
对于哈希:
For arrays:
For hashes:
老问题,但也许对其他人有帮助。我用它创建了一个纸牌游戏,这就是 @davissp14 写的,它被称为“Fisher-Yates 算法”
现在你可以将它用作:
https://dev.to/linuxander/fisher-yates-shuffle-with-ruby-1p7h
Old question, but maybe help for someone else. I used it to create a card game, that's what @davissp14 wrote, it's called "Fisher-Yates algorithm"
Now you can use it as:
https://dev.to/linuxander/fisher-yates-shuffle-with-ruby-1p7h
如果你想打乱哈希值,但又不想重载 Hash 类,可以使用排序函数,然后使用 to_h 函数将其转换回哈希值(Ruby 2.1+):
If you want to shuffle a hash, but don't want to overload the Hash class, you can use the sort function and then convert it back to a hash with the to_h function (Ruby 2.1+):
对于数组:
array.shuffle
对于哈希:
hash.sort_by{ rand() }
For an array:
array.shuffle
For a hash:
hash.sort_by{ rand() }
另一种哈希洗牌是..
An alternative hash shuffle is ..