开发者

Using array.delete(array.min) to delete one instance of a value

开发者 https://www.devze.com 2023-03-16 06:15 出处:网络
I\'m trying to create a dice game program. I want to select one of the lowest out of five die, move it to a new array and remove the singular occurrence of that lowest dice from the original array. Th

I'm trying to create a dice game program. I want to select one of the lowest out of five die, move it to a new array and remove the singular occurrence of that lowest dice from the original array. The problem is if there are more than one of the lowest die, this happens:

开发者_StackOverflow
array = %w[1,1,2,3,4]
new_array=[]
new_array.push(array.min) => 1
array.delete(array.min) => 2,3,4


new_array = array.sort.slice!(0..0)

or

new_array.push(array.sort!.shift)
0

精彩评论

暂无评论...
验证码 换一张
取 消