2

I have several ruby functions and want to check that the input is correct and also whether the input makes sense. What is a sensible way to do that?

Here's an example with one of the functions I have and what I would like to check

# Converts civil time to solar time
# civilT: Time object
# longitude: float
# timezone: fixnum
def to_solarT(civilT,longitude,timezone)
    # pseudo code to check that input is correct
    assert(civilT.class == Time.new(2013,1,1).class)
    assert(longitude.class == 8.0.class)
    assert(timezone.class == 1.class)

    # More pseudocode to check if the inputs makes sense, in this case 
    # whether the given longitude and timezone inputs make sense or whether 
    # the timezone relates to say Fiji and the longitude to Scotland. Done 
    # using the imaginary 'longitude_in_timezone' function
    assert(longitude_in_timezone(longitude,timezone))
end

I found a related question here: how to put assertions in ruby code. Is this the way to go or are there better ways to test function input in ruby?

Community
  • 1
  • 1
pandita
  • 4,739
  • 6
  • 29
  • 51
  • 1
    `1.class` makes me shiver. `Time.new.class` makes me want to scream and headdesk. – John Dvorak Jun 30 '13 at 07:01
  • 1
    Why don't your trust your program to pass valid values for the method signature? Rely on unit tests or exceptions, unless you're dealing with tainted data. – Todd A. Jacobs Jun 30 '13 at 07:38

2 Answers2

3

You shouldn't do it this way. Ruby relies heavily on duck-typing. That is, if it quacks like a duck, it is a duck. i.e. Just use the objects you receive and if they do respond correctly, it's fine. If they don't, you can rescue a NoMethodError and show the appropriate output.

Pedro Nascimento
  • 13,136
  • 4
  • 36
  • 64
  • Thanks @pedro for pointing me to duck typing.... heard about it, ignored it so far but will have a look to see what it means. – pandita Jun 30 '13 at 09:50
  • Duck typing is when you don't care much about if something is of a specific class, you just expect a behavior, i.e. does the object quack? then just ask it to quack. Don't ask if it is a Duck because you want to use the method quack. – Pedro Nascimento Jul 02 '13 at 05:15
  • 1
    Ah ok... not so hard to understand afterall :) Cheers – pandita Jul 03 '13 at 12:34
3

assert is not standard Ruby method, and is often used by test frameworks, so I don't think it is good to put it in code. Also, it does not make sense to create instances of the classes you want to check the arguments against. More straightforwardly,

def to_solarT civilT, longitude, timezone
  raise "Argument error blah blah" unless Time === civilT
  raise "Argument error blah blah" unless Float === longitude
  raise "Argument error blah blah" unless Fixnum === timezone
  ...
end
sawa
  • 165,429
  • 45
  • 277
  • 381