3

If I run the below in irb:

x = 1
until x > 7 do puts(x += 1) end

It'll print out 2-8 on new lines just as desired fine, but if I use { } syntax instead, like this:

x = 1
until x > 7 { puts(x += 1) }

I doesn't work, with a syntax error:

SyntaxError: unexpected '}', expecting end-of-input
until x>(7) { puts(x += 1) }

I'd like to understand why, as I thought that do end was analagous to { }, and that most methods would yield their output to a block if asked, but I must be wrong.

On looking at this further I notice that with the above code working in do/end guise there's no block |variable| needed for it to run, perhaps because the >(7) method call on x only puts out boolean true or false, which it doesn't yield to the block. Perhaps no methods that output booleans will accept block arguments, but then the do/end guise above is still a 'block argument' being passed isn't it?

Clearly I'm not fully understanding what exactly's going on here. Can anyone help explain this to me please? Thank you.

jbk
  • 1,911
  • 19
  • 36

0 Answers0