Skip to content

Instantly share code, notes, and snippets.

@marinados
Created January 20, 2016 10:06
Show Gist options
  • Save marinados/baeede8c874c79677073 to your computer and use it in GitHub Desktop.
Save marinados/baeede8c874c79677073 to your computer and use it in GitHub Desktop.
cool native ruby methods to use
# using bsearch wherenever possible :)
require 'benchmark'
data = (0..50_000_000)
Benchmark.bm do |x|
x.report(:find) { data.find {|number| number > 40_000_000 } }
x.report(:bsearch) { data.bsearch {|number| number > 40_000_000 } }
end
user system total real
find 3.020000 0.010000 3.030000 (3.028417)
bsearch 0.000000 0.000000 0.000000 (0.000006)
# creating an Array with a block
@board ||= Array.new(8) { Array.new(8) { '0' } }
# <=>
# To illustrate how it works, let's look at how it behaves for Fixnums.
# If you call 5<=>5, it returns 0.
# If you call 4<=>5, it returns -1.
# If you call 5<=>4, it returns 1.
# Basically, if the two numbers are the same, it returns 0, otherwise it returns -1 for least to greatest sorting, and 1 for reverse sorting.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment