Syntax error with blocks

Discussion in 'Ruby' started by Oliver Sauders, Jun 17, 2007.

  1. 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/.
     
    Oliver Sauders, Jun 17, 2007
    #1
    1. Advertising

  2. Oliver Sauders

    Banzai Guest

    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
     
    Banzai, Jun 17, 2007
    #2
    1. Advertising

  3. Oliver Sauders

    Eric I. Guest

    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 <> 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' }
     
    Eric I., Jun 17, 2007
    #3
  4. Oliver Sauders

    ole __ Guest

    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/.
     
    ole __, Jun 18, 2007
    #4
  5. Oliver Sauders

    CHubas Guest

    On Jun 18, 3:11 am, ole __ <> 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
     
    CHubas, Jun 19, 2007
    #5
  6. Oliver Sauders

    ole __ Guest

    ole __, Jun 19, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Arjen
    Replies:
    3
    Views:
    460
    Scott Allen
    Feb 27, 2005
  2. r

    New syntax for blocks

    r, Nov 10, 2009, in forum: Python
    Replies:
    46
    Views:
    1,017
    Carl Banks
    Nov 19, 2009
  3. matt
    Replies:
    1
    Views:
    287
    George Ogata
    Aug 6, 2004
  4. Steven Taylor
    Replies:
    9
    Views:
    276
    Brian Candler
    Apr 27, 2009
  5. Mark Richards
    Replies:
    3
    Views:
    335
    Tad McClellan
    Nov 18, 2007
Loading...

Share This Page