Forum: Ruby Syntax error with blocks

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ole O. (Guest)
on 2007-06-17 19:05
OK guys, wtf am I doing wrong here?
The manual says I should be able to do this.

=======================

irb(main):150:0> { puts 'foo' }
SyntaxError: compile error
(irb):150: syntax error, unexpected tSTRING_BEG, expecting kDO or '{' or
'('
{ puts 'foo' }
        ^
(irb):150: syntax error, unexpected '}', expecting $end
  from (irb):150
  from :0
irb(main):151:0> { puts('foo') }
SyntaxError: compile error
(irb):151: odd number list for Hash
  from (irb):151
  from :0
irb(main):158:0> do puts 'foo' end
SyntaxError: compile error
(irb):158: syntax error, unexpected kDO
do puts 'foo' end
  ^
(irb):158: syntax error, unexpected kEND, expecting $end
  from (irb):158
  from :0
irb(main):160:0> do; puts 'foo'; end
SyntaxError: compile error
(irb):160: syntax error, unexpected kDO
do; puts 'foo'; end
  ^
(irb):160: syntax error, unexpected kEND, expecting $end
  from (irb):160
  from :0
Banzai (Guest)
on 2007-06-17 19:15
(Received via mailing list)
On 2007-06-17 10:05:37 -0500, Oliver S. <removed_email_address@domain.invalid>
said:

>         ^
> (irb):158: syntax error, unexpected kDO
> (irb):160: syntax error, unexpected kEND, expecting $end
>   from (irb):160
>   from :0

A code block is not a valid statement.  If you want to create a Proc
object, use lambda.

irb(main):001:0> x = lambda { puts 'foo' }
=> #<Proc:0x0035393c@(irb):1>
irb(main):002:0> x.call
foo
=> nil
Ole O. (Guest)
on 2007-06-17 19:53
Thanks
Eric I. (Guest)
on 2007-06-18 01:32
(Received via mailing list)
The other thing to keep in mind is that blocks are parameters passed
into methods, so they're always associated with a method call.  For
example:

10.times { puts 'foo' }

The block is a parameter to the times method that is being called on
the Integer 10.

Even in Banzai's example:

x = lambda { puts 'foo' }

The block is passed to a method named lambda that returns a Proc of
the block passed in.

Eric
----
Are you interested in on-site Ruby training that uses well-designed,
real-world, hands-on exercises? http://LearnRuby.com
Ole O. (Guest)
on 2007-06-18 12:11
Cool. I'm guessing something like this:

1.times { puts 'foo' }

or

lamba { puts 'foo' }.call

is never really used but could be used to achieve block scope.
Ruben M. (Guest)
on 2007-06-19 05:21
(Received via mailing list)
On Jun 18, 3:11 am, ole __ <removed_email_address@domain.invalid> wrote:
> Cool. I'm guessing something like this:
>
> 1.times { puts 'foo' }
>
> or
>
> lamba { puts 'foo' }.call
>
> is never really used but could be used to achieve block scope.


If you want a more block like behavior, use begin/end

begin
  puts 'Hello from block'
end
Ole O. (Guest)
on 2007-06-19 12:01
Ahhh very nice. Thanks
This topic is locked and can not be replied to.