我可以告诉Ruby方法期望特定的参数类型吗? [英] Can I tell a Ruby method to expect a specific parameter type?

查看:83
本文介绍了我可以告诉Ruby方法期望特定的参数类型吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

def doSomething(value)
    if (value.is_a?(Integer))
        print value * 2
    else
        print "Error: Expected integer value"
        exit
    end
end

我可以告诉Ruby方法某个参数应该是一个整数,否则会崩溃吗?像Java.

Can I tell a Ruby method that a certain parameter should be an Integer, otherwise crash? Like Java.

推荐答案

不,您不能.您只能做已经在做的事情:自己检查类型.

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

这篇关于我可以告诉Ruby方法期望特定的参数类型吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆