What is the equivalent of Ruby block_given?

Oops, i quickly checked same SO author another answer before. but that code not work, i link that answer back here.

Following code work well on Crystal 1.6.1

# (1)
def foo(sock_path, &block : Int32 -> Nil)
  puts "11111"
  foo(sock_path, block)
end

# (2)
def foo(sock_path, block : Proc(Int32, Nil)? = nil)
  puts "22222"
  if block
    block.call(100)
  end
end

# both works

foo "some/path", ->(x : Int32) { puts x }

foo "some/path" do |x|
  puts x
end

# The interest things is: this actually invoke above (2)
foo("some/path")


Thanks

3 Likes