Skip to content

Instantly share code, notes, and snippets.

@pokk
Created July 13, 2017 06:32
Show Gist options
  • Save pokk/e2b6eccd084c146a6081f8e9cbf1cf75 to your computer and use it in GitHub Desktop.
Save pokk/e2b6eccd084c146a6081f8e9cbf1cf75 to your computer and use it in GitHub Desktop.
Do..End v.s Braces

What's difference between braces and do..end

Actually, there're no so big difference. But the priority is difference. We can check an example out as below then we can understand.

my_array = [1, 2, 3, 4, 5]

# <1>
p my_array.map { |item|
  item * 2
}
# >> [2, 4, 6, 8, 10]

# <2>
p my_array.map do |item|
  item * 2
end
# >> #<Enumerator: [1, 2, 3, 4, 5]:map>

Why?

The interpreter will check as like.

First <1>

p(my_array.map do |item| item * 2 end)

Second <2>

p(my_array.map) do |item| item * 2 end

Idoms

Most of people said

  • braces {} is used in SINGLE LINE.
  • do..end is used in MULTI LINE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment