Created
January 27, 2011 17:31
-
-
Save daniel-g/798853 to your computer and use it in GitHub Desktop.
Random Enumerable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Random | |
include Enumerable | |
def initialize(limit) | |
@limit = limit | |
end | |
def each(counter = 10) | |
i = 0 | |
while i < counter | |
yield rand(@limit) | |
i += 1 | |
end | |
end | |
end | |
r = Random.new(100) | |
r.each{|ran| puts ran} | |
puts "-----" | |
puts r.max | |
puts r.min | |
puts "-----" | |
puts r.sort | |
puts r.sort_by{|element| element%10} | |
puts "------" | |
puts r.inject(:+) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment