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

The Archive Base Latest Questions

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

I feel it is weird to create an object by calling new, but in

  • 0

I feel it is weird to create an object by calling “new”, but in class definition the initializer is called “initialize”. I tried to make it consistent(to be called “new” in both class definition and outside) so I tried the code below using alias_method:

class Class
    alias_method :initialize :new
end

class Foo
    def new
        puts "bar"
    end
end

f = Foo.new

I thought when I alias new as initialize, it will call initialize, and initialize is the alias of new so it will call new. But it is not working, and give errors:

SyntaxError: test.rb:2: syntax error, unexpected ':'

    alias_method :initialize :new
                              ^

Apparently new is a key word and cannot be aliased. But any way around?


Update:

Sorry I missed the comma in the alias_method

After adding the comma, the script runs without error. But it does not output “bar” so new method is not called. I am not sure why this will not work as comments below…

  • 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-31T11:01:06+00:00Added an answer on May 31, 2026 at 11:01 am

    I have no idea what the implications are for overriding Object#initialize. I wouldn’t sleep well at night with this in my code but how about something like:

    class Object
      def initialize
        new if self.respond_to?(:new)
      end
    end
    
    class Foo
      def new
        puts "BAR"
      end
    end
    
    Foo.new
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I feel this might be a weird/stupid question, but here goes... In the question
Lately, I've been going through a pretty weird phase. I feel the need to
Feel free to close and/or redirect if this has been asked, but here's my
I feel like a fool, but here goes: public interface IHasErrorController{ ErrorController ErrorController {
I feel like I should know this, but I haven't been able to figure
I feel like I'm re-inventing the wheel here, but I need to find a
I am experiencing something weird and have a workaround already, but I don't think
I'm trying to make my first application using Objective C + Core Data, but
Right now I'm having a weird problem with my own written session class, the
This morning, in Visual Studio 2005, I tried adding a new private member variable

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.