Making a dice in ruby language
Im coding a dice in ruby in which when i rolled it, it would either come up north south east or west.
what im having trouble with is when i roll it how do i tell it to return to 开发者_高级运维me one of these directions?
any help would be great.
Use Array.sample
:
[ 'north', 'south', 'east', 'west' ].sample
It doesn't get much easier than %w(north south east west).sample
(that's 1.9 method btw). Another thing that works in 1.8 and 1.9 is %w(north south east west)[rand(4)]
.
精彩评论