Skip to content

Instantly share code, notes, and snippets.

@niko79542
Created July 17, 2016 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niko79542/002809019aae7181a9a8de057077f175 to your computer and use it in GitHub Desktop.
Save niko79542/002809019aae7181a9a8de057077f175 to your computer and use it in GitHub Desktop.
passing optional block to a method
class Array
def bubble_sort!
last_unsorted = length - 1
while last_unsorted > 0
idx = 0
while idx < last_unsorted
if block_given?
switch = yield(self[idx], self[idx + 1])
else
switch = lambda { |a, b| a <=> b }.call(self[idx], self[idx + 1])
end
if switch == 1
self[idx] , self[idx + 1] = self[idx + 1], self[idx]
end
idx += 1
end
last_unsorted -= 1
end
self
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment