出于学习目的,这叫什么?是创建对象是数组还是哈希?
- stack_of_cards = []
这就是我填写它的方式:
- stack_of_cards << Card.new("A","Spades",1)
- stack_of_cards << Card.new("2",2)
- stack_of_cards << Card.new("3",3)
- ...
这是我的卡类:
- 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)
有什么建议?
解决方法
- stack_of_cards.shuffle
它是一个数组,有关详细信息,请参阅http://www.ruby-doc.org/core-1.8.7/classes/Array.html.
我已经编写了functional表单,它返回一个新的数组,并且它是一个新的数据.您可以改为使用:
- stack_of_cards.shuffle!
……将阵列原位洗牌.