Skip to content

Instantly share code, notes, and snippets.

@gamov
Created September 18, 2015 05:27
Show Gist options
  • Save gamov/96e7c36beababe48719a to your computer and use it in GitHub Desktop.
Save gamov/96e7c36beababe48719a to your computer and use it in GitHub Desktop.
Default value for an implicit block
def assert_sorted array, &block
block ||= lambda {|i,j| i+1 < j+1}
assert array.each_cons(2).all?(&block), 'Result data not correctly sorted'
end
assert_sorted [1,3,5] {|i,j| i+1 < j+1} #works
assert_sorted [1,3,5] #triggers:
ArgumentError: wrong number of arguments (1 for 2)
ruby.rb:2:in `block in assert_sorted'
ruby.rb:3:in `each'
ruby.rb:3:in `each_cons'
ruby.rb:3:in `each'
ruby.rb:3:in `all?'
ruby.rb:3:in `assert_sorted'
ruby.rb:7:in `test_scopes'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment