Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Multiple dispatch

  • Multi-dispatch functions with same name but different signature
def welcome(name : String)
  puts "welcome string"
end

def welcome(number : Int32)
  puts "welcome integer"
end

welcome("Foo")
welcome(42)
  • Integers are also accepted when we are expecting floats
def add(x : Int32, y : Int32)
  puts "handling two integers"
end

def add(x : Float64, y : Float64)
  puts "handling two floats"
end

add(2, 3)
add(2.1, 3.1)
add(2.1, 3)