The Crystal Programming Language Forum

A || b doesn't work as expected in a block

Is this a bug or something I did wrong?

def foo(&block : -> String)
  block.call
end

s = nil

foo do
  s || "foo"
end

Outputs:

7 | foo do
     ^
Error: expected block to return String, not (String | Nil)

It’s this: https://github.com/crystal-lang/crystal/issues/3093

As a workaround you can assign s to a local variable inside the block, then do what you do now.