def doSomething(value) if (value.is_a?(Integer)) print value * 2 else print "Error: Expected integer value" exit end end
Is it possible to tell the Ruby method that a certain parameter must be Integer, otherwise crash? Like Java.
No, you canβt. You can only do what you already do: check the type yourself.
I'm late to the party, but I wanted to add something else:
Ruby Duck Typing. , , , . , (*). , .
- Ruby #responds_to?, #is_a?
#responds_to?
#is_a?
, .
You can throw an exception at any time arbitrarily if you consider it necessary.
def doSomething(value) if (value.is_a?(Integer)) print value * 2 else raise "Expected integer value" end end
Regardless of whether you want to do this, this is a separate issue. :)