Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Syntax error with blocks

Reply
Thread Tools

Syntax error with blocks

 
 
Oliver Sauders
Guest
Posts: n/a
 
      06-17-2007
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

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Banzai
Guest
Posts: n/a
 
      06-17-2007
On 2007-06-17 10:05:37 -0500, Oliver Sauders <> said:

> 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


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

--
H. Asari

 
Reply With Quote
 
 
 
 
Eric I.
Guest
Posts: n/a
 
      06-17-2007
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

On Jun 17, 11:05 am, Oliver Sauders <oliver.saund...@gmail.com> wrote:
> 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' }



 
Reply With Quote
 
ole __
Guest
Posts: n/a
 
      06-18-2007
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.

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
CHubas
Guest
Posts: n/a
 
      06-19-2007
On Jun 18, 3:11 am, ole __ <oliver.saund...@gmail.com> 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

 
Reply With Quote
 
ole __
Guest
Posts: n/a
 
      06-19-2007
Ahhh very nice. Thanks

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Methods and blocks - not that clear when blocks passed into Steven Taylor Ruby 9 04-27-2009 08:46 AM
Syntax error? What syntax error? Assignment fo default values? Mark Richards Perl Misc 3 11-18-2007 05:01 PM
"Building Blocks" are "Application Blocks" Arjen ASP .Net 3 02-27-2005 01:06 AM
procs/blocks - blocks with procs, blocks with blocks? matt Ruby 1 08-06-2004 01:33 AM



Advertisments