Can I tell a Ruby method to expect a specific parameter type?

前端 未结 4 2255
旧时难觅i
旧时难觅i 2021-02-20 06:46
def doSomething(value)
    if (value.is_a?(Integer))
        print value * 2
    else
        print \"Error: Expected integer value\"
        exit
    end
end

4条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-20 07:27

    No, you can't. You can only do what you're already doing: check the type yourself.

提交回复
热议问题