Skip to content

Commit

Permalink
Add Array#sample
Browse files Browse the repository at this point in the history
  • Loading branch information
meh committed Sep 28, 2013
1 parent 176059d commit 1a74c56
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions corelib/array.rb
Expand Up @@ -928,6 +928,19 @@ def rindex(object = undefined, &block)
}
end

def sample(n = nil)
return nil if !n && empty?
return [] if n && empty?

if n
(1 .. n).map {
self[rand(length)]
}
else
self[rand(length)]
end
end

def select(&block)
return enum_for :select unless block_given?

Expand Down

0 comments on commit 1a74c56

Please sign in to comment.