5

What is the ruby best practice for handling a situation in which an object should fail to initialize owing to being passed invalid initialize arguments?

I realize that in ruby, duck typing means that we're not supposed to be overly concerned with what variable/parameters types but rather concern ourselves with how they behave. However, I am working in MacRuby which is bridged over the Cocoa Objective-C API and some of the Cocoa methods expect typed parameters.

For example, I have a ruby class that calls into the Objective-C API and must pass it an object of the NSURL class. It looks something like this:

class Alpha
  attr_accessor :model
  def initialize(hopefully_a_NSURL)
    # bridged from Objective-C API
    @model=NSManagedObjectModel.alloc.initWithContentsOfURL(hopefully_a_NSURL)    
  end # initialize  
end 

... and I would call it like so:

#bridged from Objective-C API
u=NSURL.fileURLWithPath(p)
a=Alpha.new(u)
puts "a=#{a.model}" # => a=#<NSManagedObjectModel:0x2004970e0

>

... which works nicely.

However, if I were to slip up:

a=Alpha.new("Whoops, a string not a NSURL" )

... it explodes messily with errors coming from the depths of the Objective-C API.

I can, of course, put in test that will prevent bad parameter for reaching the bridged objects:

class Alpha
  attr_accessor :model
  def initialize(hopefully_a_NSURL)
    if hopefully_a_NSURL.class==NSURL
      @model=NSManagedObjectModel.alloc.initWithContentsOfURL(hopefully_a_NSURL) 
    end   
  end # initialize  
end 


u=NSURL.fileURLWithPath(p)
a=Alpha.new("")
puts "a=#{a}" # => a=#<Alpha:0x200399160>

... but I still get a live instance back. I even tried returning nil from initialize but it seems that ruby insist on always returning a live instance.

Everything I've read says that type checking is heavily frowned upon in ruby but perhaps I will have to make an exception in the case of MacRuby. Would this be a good use of exceptions in ruby or is there a more elegant solution? I'm a noob in ruby so assume I am approaching the problem from the wrong perspective.

TechZen
  • 64,370
  • 15
  • 118
  • 145
  • Wait, you're saying you **want** your application to silently fail if a constructor is passed invalid arguments? – Matthew Ratzloff May 14 '11 at 03:32
  • No, but I want it to fail in a controlled way that doesn't leave a live object. If I was doing this in pure Objective-C, I could return a nil for the object and test for that. – TechZen May 14 '11 at 12:39
  • I'm probably being overly paranoid and I may not even bother but once the question came up, I wanted to know how to handle it. – TechZen May 14 '11 at 12:52

1 Answers1

2

I'd try to convert the argument and raise a TypeError if no conversion was possible:

Raised when encountering an object that is not of the expected type.

[1, 2, 3].first("two")

raises the exception:

TypeError: can't convert String into Integer

The Ruby core and standard libraries do it so there's no reason you can't do it too. The Ruby core will raise exceptions when you do something you're not supposed to (calling an unsupported method, calling a method with the wrong number of arguments, ...) so throwing a TypeError would make sense. And, if TypeError isn't quite appropriate, there's always ArgumentError.

In your specific case, try to convert the argument to an NSURL by calling to_s and then instantiating an NSURL using that string if they don't give you an NSURL. I don't know my way around MacRuby or the corresponding Mac APIs so I'm sort of guessing on the sensible behavior in this specific case but I think the "convert or raise an exception" idea is sound and sensible.

Of course, you should document the behavior you're going to use in your API documentation too.

mu is too short
  • 426,620
  • 70
  • 833
  • 800
  • So, in `initialize` I would test the for the NSURL and then throw an exception if it fails. Then I would wrap the call to `initialize` in exception handlers? – TechZen May 14 '11 at 13:01
  • @TechZen: I'd probably check if it is an `NSURL`; if it is then continue; if it isn't then try to construct an `NSURL` from the argument. The "construct NSURL" branch would be wrapped in an exception handler that would translate any exceptions to a `TypeError` or `ArgumentError` and raise the translated exception. I'd probably want to be able to pass a string or an instance of the [URI](http://www.ruby-doc.org/stdlib/libdoc/uri/rdoc/classes/URI.html) classes. If you could coerce the argument to an `NSURL` then do so, otherwise raise an exception. – mu is too short May 14 '11 at 18:13
  • @TechZen: Another example would be [`Date.strptime`](http://www.ruby-doc.org/stdlib/libdoc/date/rdoc/classes/DateTime.html#M000484) which raises an `ArgumentError` exception if can't parse the date string you give it. – mu is too short May 15 '11 at 16:58
  • is to short -- Sorry I haven't had the chance to test your answer. I tried to install rmv and things went a little screwy. – TechZen May 15 '11 at 21:20
  • @TechZen: No worries, I just came across an `ArgumentError` in [this question](http://stackoverflow.com/q/6007499/479863) and thought you might be interested. – mu is too short May 15 '11 at 23:18