Hi everyone. I’m very new to ruby development. I coming form .Net side
of things. Is there any way in ruby to verify the type of whatever
parameter your passing to a method and give the user a good message. For
example:
def Add_two_numbers(x,y)
…if type of x is not integer
throw an error “x was expected to be an integer and is not”
…if type of y is not an integer
throw an error “y was expected to be an integer and is not”
end
When i just hit a bug where i was expecting an array of comments and
instead a single comment was passed (in the constructor). The code
exploded with i tried to do “@comments << comments[0]” because @comments
was not an array with an error message like "undefined function ‘<<’
which took me awhile to figure out. Want to try to prevent this in the
future if possible.
Hi everyone. I’m very new to ruby development. I coming form .Net side
of things. Is there any way in ruby to verify the type of whatever
parameter your passing to a method and give the user a good message.
Welcome to the wonderful world of Ruby!
Your Ruby experience will be much more smoothly if you get rid of the
static typing mindset. It’s not the way people use Ruby. But many
people
coming from statically typed languages fear that their Ruby programs
will
be totally unsafe if they do not ensure types are enforced.
You’ll find a lot of explanations under the term “duck typing”.
For
example:
def Add_two_numbers(x,y)
…if type of x is not integer
throw an error “x was expected to be an integer and is not”
…if type of y is not an integer
throw an error “y was expected to be an integer and is not”
end
In the specific case of your method above, I’d simply do
def add_two_numbers(x, y)
x.to_int + y.to_int
end
This will reliably fail if x or y do not represent integer values.
When i just hit a bug where i was expecting an array of comments and
instead a single comment was passed (in the constructor). The code
exploded with i tried to do “@comments << comments[0]” because @comments
was not an array with an error message like "undefined function ‘<<’
which took me awhile to figure out. Want to try to prevent this in the
future if possible.
Better use Integer for the test because Fixnums turn to Bignums from a
certain number on. Using #to_int or Integer() is a simpler approach as
it
includes the exception throwing code with a meaningful error message
already:
irb(main):023:0> Integer(“x”)
ArgumentError: invalid value for Integer(): “x”
from (irb):23:in Integer' from (irb):23 from /usr/bin/irb:12:in ’
irb(main):024:0> “x”.to_int
NoMethodError: undefined method to_int' for "x":String from (irb):24 from /usr/bin/irb:12:in ’
Why use respond_to?(:to_int) for detection if you write x.to_int + y.to_int
anyway? That’s redundant work and not Duck Typing.
Hm, true. I guess there might be cases where you want a specific
error message in the method itself, versus the method’s caller? Or
maybe if there are other implicit conversions you can fall back to.
Probably rescue-able. Agreed, though.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.