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 389133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:50:14+00:00 2026-05-12T15:50:14+00:00

New to Ruby, and I’m trying to figure out what idiom to use to

  • 0

New to Ruby, and I’m trying to figure out what idiom to use to restrict some integer values to the constructor of a class.

From what I’ve done so far, if I raise an exception in initialize(), the object is still created but will be in an invalid state (for example, some nil values in instance variables). I can’t quite see how I’m supposed to restrict the values without going into what looks unnecessarily big steps like restricting access to new().

So my question is, by what mechanism can I restrict the range of values an object is instantiated with?

  • 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-12T15:50:15+00:00Added an answer on May 12, 2026 at 3:50 pm

    Huh, you are entirely correct that the object still lives even if initialize raises an exception. However, it will be quite hard for anyone to hang on to a reference unless you leak self out of initialize like the following code I just wrote does:

    >> class X
    >>   def initialize
    >>     $me = self
    >>     raise
    >>   end
    >>   def stillHere!
    >>     puts "It lives!"
    >>   end
    >> end
    => nil
    >> t = X.new
    RuntimeError: 
        from (irb):14:in `initialize'
        from (irb):20:in `new'
        from (irb):20
    >> t
    => nil
    >> $me
    => #<X:0xb7ab0e70>
    >> $me.stillHere!
    It lives!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im new to ruby and rails, however I cant figure out why this doesnt
Brand new to Ruby. Am trying to sort a de-duplicated list of email FROM
I'm brand new to ruby and rails and I'm having trouble creating a sign-out
I am a new Ruby programmer and have been looking through some tutorials for
I'm a new ruby/rails user trying to get my mac set up for the
I am starting out to play around with Ruby and trying to make a
new to Ruby so I'm still getting to grips with some of the Rails
I'm currently finishing up testing a new Ruby on Rails app. Just recently, some
I am looking for NEW Ruby on Rails resource websites (Screencasts, Tutorials, etc) some
I am trying to get familiar with the new ruby selenium-webdriver as it appears

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.