Skip to content

Instantly share code, notes, and snippets.

@groteck
Last active April 11, 2016 17:48
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 groteck/4056270eddf90baa9280db04bd994f09 to your computer and use it in GitHub Desktop.
Save groteck/4056270eddf90baa9280db04bd994f09 to your computer and use it in GitHub Desktop.
Blocks with "do" are different to Blocks with "{}"
#!/usr/bin/env ruby
puts "Count with {}"
puts [[1,1],[2],[3],[4,3]].count { |elements| !elements.all? { |element| element == elements.first } or elements.size == 1 }
puts "----------------------"
# Response:
# Count with {}
# 3
# ----------------------
puts "Count with do"
puts [[1,1],[2],[3],[4,3]].count do |elements|
!elements.all? { |element| element == elements.first } or elements.size == 1
end
puts "----------------------"
# Response:
# Count with do
# 4
# ----------------------
puts "Map with {}"
puts [[1,1],[2],[3],[4,3]].map { |elements| !elements.all? { |element| element == elements.first } or elements.size == 1 }
puts "----------------------"
# Response:
# Map with {}
# false
# true
# true
# true
# ----------------------
puts "Map with do"
puts [[1,1],[2],[3],[4,3]].map do |elements|
!elements.all? { |element| element == elements.first } or elements.size == 1
end
puts "----------------------"
# Response:
# Map with do
# #<Enumerator:0x00000001dd68b8>
# ----------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment