# What is the difference between two block?

**URL:** <https://www.ruby-forum.com/t/what-is-the-difference-between-two-block/118707>\
**Category:** Ruby\
**Created:** [October 30, 2007, 10:03am UTC](https://www.ruby-forum.com/t/what-is-the-difference-between-two-block/118707 "2007-10-30T10:03:30Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![vellingiri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/5fc32e/32.png) [@vellingiri](https://www.ruby-forum.com/u/vellingiri)\
**Post date:** [October 30, 2007, 10:03am UTC](https://www.ruby-forum.com/t/what-is-the-difference-between-two-block/118707/1 "2007-10-30T10:03:30Z")

</div>

Dear friends,  
What is the difference between do … end block and { … }.  
Is there any big difference.while doign the program more than one do and  
also we can have inside one do … end.  
like this  
do  
puts hai  
do  
puts  
end  
end  
And aslo ,same as the { … ; {…} }  
I wan to know the difference,I feel soething is there,but i am not able  
to findout the answers for this.  
Anybody help me please.

by  
vellingiri.

---

<div class="post-metadata">

**Author:** ![vellingiri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/5fc32e/32.png) [@vellingiri](https://www.ruby-forum.com/u/vellingiri)\
**Post date:** [October 30, 2007, 10:12am UTC](https://www.ruby-forum.com/t/what-is-the-difference-between-two-block/118707/2 "2007-10-30T10:12:43Z")

</div>

Arul hari wrote:

> What is the difference between do … end block and { … }.

Precedence. Other than that there is no difference, but it’s convention  
to use  
{} for single-line blocks and do end for multi-line blocks.

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![vellingiri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/5fc32e/32.png) [@vellingiri](https://www.ruby-forum.com/u/vellingiri)\
**Post date:** [October 30, 2007, 10:25am UTC](https://www.ruby-forum.com/t/what-is-the-difference-between-two-block/118707/3 "2007-10-30T10:25:52Z")

</div>

Sebastian H. wrote:

> Arul hari wrote:
> 
> > What is the difference between do … end block and { … }.
> 
> Precedence. Other than that there is no difference, but it’s convention  
> to use  
> {} for single-line blocks and do end for multi-line blocks.
> 
> HTH,  
> Sebastian

Dear friend,  
How do you say single line blocks and multi-line blocks.  
We can use both the methods are multi-line blocks.  
could you say some example.  
I can’t understand what you are saying?

by  
vellingiri.

---

<div class="post-metadata">

**Author:** ![vellingiri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/5fc32e/32.png) [@vellingiri](https://www.ruby-forum.com/u/vellingiri)\
**Post date:** [October 30, 2007, 10:41am UTC](https://www.ruby-forum.com/t/what-is-the-difference-between-two-block/118707/4 "2007-10-30T10:41:56Z")

</div>

Arul hari wrote:

> Sebastian H. wrote:
> 
> > it’s convention to use  
> > {} for single-line blocks and do end for multi-line blocks.
> 
> ```
> How do you say single line blocks and multi-line blocks.
> 
> ```

Ehrm, I open my mouth and the words come out? I don’t quite understand  
your question.

> We can use both the methods are multi-line blocks.

You can use {} as well as do end for multi-line blocks, yes (I’m  
assuming  
that’s what you meant to convey with the above sentence, although  
honstly I  
had some trouble parsing that). But it’s _convention_ to use do end for  
multi-line blocks. It’s only convention, it’s not enforced by ruby. As I  
said: the only real difference is precedence.

> could you say some example.

10.times {|i|  
bar=something(i)  
foo=bar.some\_thing\_else  
puts foo  
} # Discouraged

10.times do |i|  
bar=something(i)  
foo=bar.some\_thing\_else  
puts foo  
end # Encouraged

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![vellingiri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/5fc32e/32.png) [@vellingiri](https://www.ruby-forum.com/u/vellingiri)\
**Post date:** [October 30, 2007, 11:02am UTC](https://www.ruby-forum.com/t/what-is-the-difference-between-two-block/118707/5 "2007-10-30T11:02:35Z")

</div>

Sebastian H. wrote:

> Arul hari wrote:
> 
> > Sebastian H. wrote:
> > 
> > > it’s convention to use  
> > > {} for single-line blocks and do end for multi-line blocks.
> > 
> > ```
> > How do you say single line blocks and multi-line blocks.
> > 
> > ```
> 
> Ehrm, I open my mouth and the words come out? I don’t quite understand  
> your question.
> 
> > We can use both the methods are multi-line blocks.
> 
> You can use {} as well as do end for multi-line blocks, yes (I’m  
> assuming  
> that’s what you meant to convey with the above sentence, although  
> honstly I  
> had some trouble parsing that). But it’s _convention_ to use do end for  
> multi-line blocks. It’s only convention, it’s not enforced by ruby. As I  
> said: the only real difference is precedence.
> 
> > could you say some example.
> 
> 10.times {|i|  
> bar=something(i)  
> foo=bar.some\_thing\_else  
> puts foo  
> } # Discouraged
> 
> 10.times do |i|  
> bar=something(i)  
> foo=bar.some\_thing\_else  
> puts foo  
> end # Encouraged
> 
> HTH,  
> Sebastian

Dear friends,

```
 Thanks all of guys especially for sebastian for his deep 

```

explanation.

by  
vellingiri.

---

<div class="post-metadata">

**Author:** ![vellingiri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/5fc32e/32.png) [@vellingiri](https://www.ruby-forum.com/u/vellingiri)\
**Post date:** [October 30, 2007, 11:21am UTC](https://www.ruby-forum.com/t/what-is-the-difference-between-two-block/118707/6 "2007-10-30T11:21:09Z")

</div>

From: Arul hari [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# I wan to know the difference,I feel soething is there,but i

# am not able to findout the answers for this.

there is difference, but not much

> def f a,b  
> yield a,b  
> end

precedence issue:

> f 1,2 {|x,y| p x,y}  
> SyntaxError: compile error  
> (irb):33: syntax error, unexpected ‘{’, expecting $end  
> f 1,2 {|x,y| p x,y}  
> ^

> f 1,2 do|x,y| p x,y end  
> 1  
> 2

as always, it is safe by enclosing parameters w parens

irb(main):035:0\> f(1,2) {|x,y| p x,y}  
1  
2

irb(main):036:0\> f(1,2) do|x,y| p x,y end  
1  
2

also, one-liner chain fans sees do-end as noisy and sensitive to spacing  
(since do-ends are keywords)

irb(main):037:0\> [1,2,3].map{|x| 2_x}.map{|x|x+1}  
=\> [3, 5, 7]  
irb(main):038:0\> [1,2,3].map do|x| 2_x end.map do |x|x+1 end  
=\> [3, 5, 7]  
irb(main):039:0\> [1,2,3].mapdo|x| 2_x end.map do |x|x+1 end  
SyntaxError: compile error  
(irb):39: syntax error, unexpected kEND, expecting $end  
[1,2,3].mapdo|x| 2_x end.map do |x|x+1 end  
^

nonetheless, i like both. i particularly like do-end since i like typing  
fast without using shift. but if you have editors like textmate or  
similar, no need 🙂

kind regards -botp

---

<div class="post-metadata">

**Author:** ![vellingiri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/5fc32e/32.png) [@vellingiri](https://www.ruby-forum.com/u/vellingiri)\
**Post date:** [October 30, 2007, 12:10pm UTC](https://www.ruby-forum.com/t/what-is-the-difference-between-two-block/118707/7 "2007-10-30T12:10:17Z")

</div>

On 10/30/07, Sebastian H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Arul hari wrote:
> 
> > What is the difference between do … end block and { … }.
> 
> Precedence. Other than that there is no difference, but it’s convention to use  
> {} for single-line blocks and do end for multi-line blocks.

I fail to see that convention often personally I try to adhere to a  
different convention I have seen recently:

I use {} if the value of the block is used and do end when it is all  
about side effects,

example:

[…].map{|x| x.to\_s}

# note that under that convention map do end disappears almost

vs.

[…].each do | x |  
puts x  
end # yes I know that puts […] does the same ,)

HTH  
Robert

---

<div class="post-metadata">

**Author:** ![vellingiri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/5fc32e/32.png) [@vellingiri](https://www.ruby-forum.com/u/vellingiri)\
**Post date:** [October 30, 2007, 1:57pm UTC](https://www.ruby-forum.com/t/what-is-the-difference-between-two-block/118707/8 "2007-10-30T13:57:33Z")

</div>

PeÃ±a, Botp wrote:

> From: Arul hari [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]
> 
> # I wan to know the difference,I feel soething is there,but i
> 
> # am not able to findout the answers for this.
> 
> there is difference, but not much
> 
> > def f a,b  
> > yield a,b  
> > end
> 
> precedence issue:
> 
> > f 1,2 {|x,y| p x,y}  
> > SyntaxError: compile error  
> > (irb):33: syntax error, unexpected ‘{’, expecting $end  
> > f 1,2 {|x,y| p x,y}  
> > ^
> 
> > f 1,2 do|x,y| p x,y end  
> > 1  
> > 2
> 
> as always, it is safe by enclosing parameters w parens
> 
> irb(main):035:0\> f(1,2) {|x,y| p x,y}  
> 1  
> 2
> 
> irb(main):036:0\> f(1,2) do|x,y| p x,y end  
> 1  
> 2
> 
> also, one-liner chain fans sees do-end as noisy and sensitive to spacing  
> (since do-ends are keywords)
> 
> irb(main):037:0\> [1,2,3].map{|x| 2_x}.map{|x|x+1}  
> =\> [3, 5, 7]  
> irb(main):038:0\> [1,2,3].map do|x| 2_x end.map do |x|x+1 end  
> =\> [3, 5, 7]  
> irb(main):039:0\> [1,2,3].mapdo|x| 2_x end.map do |x|x+1 end  
> SyntaxError: compile error  
> (irb):39: syntax error, unexpected kEND, expecting $end  
> [1,2,3].mapdo|x| 2_x end.map do |x|x+1 end  
> ^
> 
> nonetheless, i like both. i particularly like do-end since i like typing  
> fast without using shift. but if you have editors like textmate or  
> similar, no need 🙂
> 
> kind regards -botp

Dear Friend,  
You have told me that with example,but that is not suitable for me.  
I have not changed anything from that,simple I have but one bracket.  
It will not shows any error.  
So,Your example also not suitable for my questions.  
Please tell me some more examples with exaplantion.

def f a,b  
yield a,b  
end  
nil  
f 1,2 {|x,y| p x,y}  
SyntaxError: compile error  
(irb):4: syntax error, unexpected ‘{’, expecting $end  
f 1,2 {|x,y| p x,y}  
^  
from (irb):4  
from :0  
f 1,2 do|x,y| p x,y end  
1  
2  
nil  
f (1,2) {|x,y| p x,y}  
(irb):6: warning: don’t put space before argument parentheses  
1  
2  
nil  
f (1,2){|x,y| p x,y}  
(irb):7: warning: don’t put space before argument parentheses  
1  
2  
nil
