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

  • SEARCH
  • Home
  • 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 8736123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:13:57+00:00 2026-06-13T10:13:57+00:00

When I do the following in irb I get this output: >> class TestMe

  • 0

When I do the following in irb I get this output:

>> class TestMe
>>   def new
>>     puts 'hi!'
>>   end
>> end
=> nil
>> TestMe.new.new
hi!

Additionally:

>> class TestMe
>> end
=> nil
>> TestMe.new.new
NoMethodError: undefined method `new' for #<TestMe:0x00000101038750>

But when I’m writing some code in my text editor of choice that is calling an instance method named new (but is not the Object method new that instantiates new objects) it highlights new as if it were a reserved keyword:

@page = current_user.locations.new

Note that locations here returns a delegator class that does some heavy lifting (via this new method) and eventually does return a Location.new instance with some basic setup data ready to go, but new is not itself being called on a class object. Is this an acceptable use of the method name or will I run into issues with it?

  • 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-06-13T10:13:58+00:00Added an answer on June 13, 2026 at 10:13 am

    The first call to .new will invoke the constructor, and return an instance of your class. The second call to .new will invoke an instance method on that object. It’s completely acceptable to define a new instance method.

    In order to interfere with the constructor, you would have to define a class-level method called new. That method can invoke super#new (which invokes Class#new) to perform the actual creation of the object:

    class Test
      def self.new
        puts "hi!"
        super
      end
    end
    
    
    x = Test.new # outputs "hi"
    

    It’s completely valid to overwrite new at both an instance and class level, so long as you define your custom new method to do something sane.

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

Sidebar

Related Questions

Consider the following: irb(main):001:0> class A irb(main):002:1> def initialize irb(main):003:2> @string = my string
Following are the simple statements in the irb shell. What does nil in the
I'm trying to do the following: page = Mechanize.new.get https://sis-app.sph.harvard.edu:9030/prod/bwckschd.p_disp_dyn_sched But I only get
This code works in irb: irb(main):037:0> eval <<-EOS irb(main):038:0 #{attribute} = host irb(main):039:0 puts
I implemented the Greedy algorithim in ruby using the following code: class Greedy def
module CallableDict def method_missing method puts x[#{method}] end end The above code is my
In the following program (written in irb shell) i call a method hidden of
Can anyone shed some insight onto why the following happens? irb(main):001:0> r = '1'..'30'
Following code worked fine abstract class FunctionRunnable<V> implements Runnable { protected abstract V calculate();
Following leads in this thread and others, I've set up a code block where

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.