I want to know if there is a much cleaner way of doing this. Basically, I want to pick a random element from an array of variable length. Normally, I would do it like this:
我想知道是否有更清洁的方法来做这件事。基本上,我想从一个可变长度的数组中选择一个随机的元素。通常我会这样做:
myArray = ["stuff", "widget", "ruby", "goodies", "java", "emerald", "etc" ]
item = myArray[rand(myarray.length)]
Is there something that is more readable / simpler to replace the second line? Or is that the best way to do it. I suppose you could do myArray.shuffle.first
, but I only saw #shuffle
a few minutes ago on SO, I haven't actually used it yet.
是否有什么东西更容易读/更容易替换第二行?或者这是最好的方法。我想你可以做我的。首先,但是我在几分钟前才看到#shuffle,我还没有实际使用它。
5 个解决方案
#1
960
Just use Array#sample
:
只使用数组#样品:
[:foo, :bar].sample # => :foo, or :bar :-)
It is available in Ruby 1.9.1+. To be also able to use it with an earlier version of Ruby, you could require "backports/1.9.1/array/sample"
.
它可以在Ruby 1.9.1+中使用。为了能够使用Ruby的早期版本,您可能需要“backports/1.9.1/array/sample”。
Note that in Ruby 1.8.7 it exists under the unfortunate name choice
; it was renamed in later version so you shouldn't use that.
请注意,在Ruby 1.8.7中,它存在于不幸的名称选择之下;它在以后的版本中被重新命名,所以你不应该使用它。
Although not useful in this case, sample
accepts a number argument in case you want a number of distinct samples.
虽然在这种情况下不太有用,但是示例接受一个数字参数,以防您需要一些不同的示例。
#2
66
myArray.sample(x)
can also help you to get x random elements from the array.
sample(x)也可以帮助您从数组中获取x随机元素。
#3
10
Random Number of Random Items from an Array
def random_items(array)
array.sample(1 + rand(array.count))
end
Examples of possible results:
my_array = ["one", "two", "three"]
my_array.sample(1 + rand(my_array.count))
=> ["two", "three"]
=> ["one", "three", "two"]
=> ["two"]
#4
7
myArray.sample
will return 1 random value.
将返回1个随机值。
myArray.shuffle.first
will also return 1 random value.
也会返回一个随机值。
#5
6
Personally, I would prefer the method item = myArray.random_element
.
就个人而言,我更喜欢方法项= myArray.random_element。
UPDATE: If you aren't using rails (active support) you'll need to define the method yourself. See Marc-André Lafortune's answer for a more modern view. You could also define the Array#sample method similar to this, if you are using ruby version < 1.9.1.
更新:如果您没有使用rails(主动支持),您需要自己定义方法。看看Marc-Andre Lafortune对更现代的观点的回答。如果您使用的是ruby版本< 1.9.1,您也可以定义类似于此的数组#样例方法。
class Array
def random_element
self[rand(length)]
end
end
#1
960
Just use Array#sample
:
只使用数组#样品:
[:foo, :bar].sample # => :foo, or :bar :-)
It is available in Ruby 1.9.1+. To be also able to use it with an earlier version of Ruby, you could require "backports/1.9.1/array/sample"
.
它可以在Ruby 1.9.1+中使用。为了能够使用Ruby的早期版本,您可能需要“backports/1.9.1/array/sample”。
Note that in Ruby 1.8.7 it exists under the unfortunate name choice
; it was renamed in later version so you shouldn't use that.
请注意,在Ruby 1.8.7中,它存在于不幸的名称选择之下;它在以后的版本中被重新命名,所以你不应该使用它。
Although not useful in this case, sample
accepts a number argument in case you want a number of distinct samples.
虽然在这种情况下不太有用,但是示例接受一个数字参数,以防您需要一些不同的示例。
#2
66
myArray.sample(x)
can also help you to get x random elements from the array.
sample(x)也可以帮助您从数组中获取x随机元素。
#3
10
Random Number of Random Items from an Array
def random_items(array)
array.sample(1 + rand(array.count))
end
Examples of possible results:
my_array = ["one", "two", "three"]
my_array.sample(1 + rand(my_array.count))
=> ["two", "three"]
=> ["one", "three", "two"]
=> ["two"]
#4
7
myArray.sample
will return 1 random value.
将返回1个随机值。
myArray.shuffle.first
will also return 1 random value.
也会返回一个随机值。
#5
6
Personally, I would prefer the method item = myArray.random_element
.
就个人而言,我更喜欢方法项= myArray.random_element。
UPDATE: If you aren't using rails (active support) you'll need to define the method yourself. See Marc-André Lafortune's answer for a more modern view. You could also define the Array#sample method similar to this, if you are using ruby version < 1.9.1.
更新:如果您没有使用rails(主动支持),您需要自己定义方法。看看Marc-Andre Lafortune对更现代的观点的回答。如果您使用的是ruby版本< 1.9.1,您也可以定义类似于此的数组#样例方法。
class Array
def random_element
self[rand(length)]
end
end