# Block.call

**URL:** <https://www.ruby-forum.com/t/block-call/180966>\
**Category:** Ruby\
**Created:** [January 2, 2010, 11:48am UTC](https://www.ruby-forum.com/t/block-call/180966 "2010-01-02T11:48:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [January 2, 2010, 11:48am UTC](https://www.ruby-forum.com/t/block-call/180966/1 "2010-01-02T11:48:01Z")

</div>

Hello gurus,

please see the code below, why passing a block to a function then call  
block.call in the function will get success, but run block.call out of  
a function will get failed?

irb(main):023:0\* def myhi(&block)  
irb(main):024:1\> block.call  
irb(main):025:1\> end  
=\> nil

irb(main):026:0\> myhi { puts “hi welcome” }  
hi welcome  
=\> nil

irb(main):027:0\> { puts “hi welcome” }.call  
SyntaxError: (irb):27: syntax error, unexpected tSTRING\_BEG, expecting  
keyword\_do or ‘{’ or ‘(’  
{ puts “hi welcome” }.call  
^  
(irb):27: syntax error, unexpected ‘}’, expecting $end  
{ puts “hi welcome” }.call  
^  
from /usr/bin/irb:12:in `’

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [January 2, 2010, 1:22pm UTC](https://www.ruby-forum.com/t/block-call/180966/2 "2010-01-02T13:22:16Z")

</div>

thanks.  
but why in the function block.call can be executed?

On Sat, Jan 2, 2010 at 7:50 PM, Robert K.

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [January 2, 2010, 12:54pm UTC](https://www.ruby-forum.com/t/block-call/180966/3 "2010-01-02T12:54:10Z")

</div>

On 01/02/2010 11:47 AM, Ruby N. wrote:

> =\> nil  
> (irb):27: syntax error, unexpected ‘}’, expecting $end  
> { puts “hi welcome” }.call  
> ^  
> from /usr/bin/irb:12:in `’

Because, as the error message says, this is not valid Ruby syntax. You  
can use “lambda” or “proc” instead:

irb(main):001:0\> lambda { puts “hi” }.call  
hi  
=\> nil  
irb(main):002:0\>

But this is really useless, i.e. in that case you should simply do “puts  
‘hi’”. The lambda notation really only makes sense if you want to store  
the block away for later usage. Note, that there is also this syntax  
(in case you want to structure your code):

begin  
puts ‘hi’  
end

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [January 2, 2010, 1:40pm UTC](https://www.ruby-forum.com/t/block-call/180966/4 "2010-01-02T13:40:25Z")

</div>

Ruby N.:

> thanks.  
> but why in the function block.call can be executed?

An incoming code block will be converted into a Proc object and bound  
^^^^^^^^^^^^^^^^^^^^^^^^^^^^  
to the block variable. You can pass it around to other methods, call it  
directlyusing call,or yield to it as though youâ€™d never bound it to a  
variable at all.
