I want to pass arguments to a Proc like using splat, but it returns an error wrong number of block arguments (1 for 0)
$callbacks = {} of String => Array(->)
def add_callback(event, &block)
begin
$callbacks[event.to_s] << block
rescue
$callbacks[event.to_s] = [block]
end
end
add_callback(:event) do |arg|
puts "event!"
end
$callbacks["event"].first.call
Error in line 11: wrong number of block arguments (1 for 0)
http://carc.in/#/r/7gw
You need to specify the argument list everywhere.
class EventManager
def initialize
@callbacks = Hash(String, Array(String ->)).new {|h, k| h[k] = [] of String -> }
end
def add(event, &callback : String ->)
@callbacks[event] << callback
end
def fire(event, argument : String)
@callbacks[event].each &.call(argument)
end
end
callbacks = EventManager.new
callbacks.add("foo") do |argument|
puts "Got #{argument}"
end
callbacks.add("bar") do
puts "I was called"
end
callbacks.fire "foo", "Ping"
callbacks.fire "bar", "Pong"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With