Skip to content

Commit

Permalink
Fixing Array#choice on 1.9
Browse files Browse the repository at this point in the history
  • Loading branch information
steveklabnik committed Oct 10, 2011
1 parent ce2dad4 commit 7632d93
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions kernel/common/array19.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,25 @@ def self.try_convert(obj)
Rubinius::Type.try_convert obj, Array, :to_ary
end

def choice(n=undefined)
raise NoMethodError if size == 0
return at(Kernel.rand(size)) if n.equal? undefined

n = Type.coerce_to(n, Fixnum, :to_int)
raise ArgumentError, "negative array size" if n < 0

n = size if n > size
result = Array.new(self)

n.times do |i|
r = i + Kernel.rand(size - i)
result.tuple.swap(i,r)
end

result[n..size] = []
result
end

def flatten(level=-1)
level = Rubinius::Type.coerce_to(level, Integer, :to_int)
return self.dup if level == 0
Expand Down

0 comments on commit 7632d93

Please sign in to comment.