Skip to content

Specifying Callback Types

mosop edited this page Jul 27, 2017 · 12 revisions

Each callback group has a callback type. The default callback type is Proc(T, Nil). T is a class that defines a callback group where callbacks are appended.

To specify a custom type, use the proc_type argument.

class AgeOfMajority
  Callback.enable
  define_callback_group :check, proc_type: Proc(Int32, String)

  on_check :smiley do |o, i|
    i >= o.age ? ":)" : ":P"
  end

  getter age : Int32

  def initialize(@age)
  end
end

aom = AgeOfMajority.new(18)
aom.run_callbacks_for_check(25) do |results|
  puts results[:smiley]
end
aom.run_callbacks_for_check(15) do |results|
  puts results[:smiley]
end

This prints:

:)
:P

Note: The first argument's type is always T.

Clone this wiki locally