Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5838993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:31:39+00:00 2026-05-22T11:31:39+00:00

What is the ruby best practice for handling a situation in which an object

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T11:31:40+00:00Added an answer on May 22, 2026 at 11:31 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm wondering what is best practive for handling this type of situation in ruby
I am very new to Ruby so could you please suggest the best practice
Is there a best practice in Ruby when it comes to explicitly specifying method
What are the best practices for reading and writing binary data in Ruby? In
What are the best Ruby / Rails book available right now ? Note :
What are the available (best) ruby IP-based geolocation gem/plugins? How do they compare to
Whats the best/easiest GUI library out there for Ruby? I would prefer a cross-platform
What's the best way to implement the enum idiom in Ruby? I'm looking for
What's the best way to write a supybot plugin in ruby? Or other language
Where is the best location to put a plain old Ruby class in a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.