Skip to content

@mysmallidea /gist:1120376
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
catch/throw
marble_jar = 0
count = 0
catch :jar_is_full do
1000000000000.times do |marble|
count += 1
# grab a random-sized marble
marble = rand(9)
if marble_jar + marble > 1000
# The jar is full
throw :jar_is_full
else
# Insert the marble
marble_jar += marble
end
end
end
puts "\n== There are #{marble_jar} marbles in the jar, but we only had to iterate #{count} times to find that out.\n\n"
# == There are 998 marbles in the jar, but we only had to iterate 258 times to find that out.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.