3 "hey, #{value} is string :)" 4 end 5 6 def string?(value : Number) : String 7 "man, #{value} is a number :|" 8 end 9 10 def string?(_value) : String 11 "I don't care" 12 end 13 end 14 15 is = IsString.new 16 puts is.string?("Hello") # hey, Hello is string :) 17 puts is.string?(9000) # man, 9000 is a number :| 18 puts is.string?(nil) # I don't care 19 puts is.string?([1,2,3]) # I don't care Listing 4: Method overloading 31 / 43
3 "hey, #{value} is string :)" 4 end 5 6 def string?(value : Number) : String 7 "man, #{value} is a number :|" 8 end 9 10 def string?(value : String, _other) : String 11 "One of them" 12 end 13 14 def string?(_other, value : String) 15 string?(value, nil) 16 end 17 18 def string?(_value) : String 19 "I don't care" 20 end 21 end 22 23 is = IsString.new 24 puts is.string?("ok", 0) # One of them 25 puts is.string?(0, "ok") # One of them 32 / 43