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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:09:30+00:00 2026-05-13T00:09:30+00:00

I encountered a strange question about override initialize message of BigDecimal. class Test1 <

  • 0

I encountered a strange question about override initialize message of BigDecimal.

class Test1 < String
  def initialize(a, b)
    puts a
    puts b
  end
end

require 'bigdecimal'
class Test2 < BigDecimal
  def initialize(a, b)
    puts a
    puts b
  end
end

>> Test1.new('a', 'b')
a
b
>> Test2.new('a', 'b')
TypeError: wrong argument type String (expected Fixnum)
    from (irb):17:in `new'
    from (irb):17

Why I can override the initialize message of String, but not of BigDecimal?

  • 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-13T00:09:30+00:00Added an answer on May 13, 2026 at 12:09 am

    When you look into sources of Ruby classes, you’ll see that the String class defines method String#initialize, which is called after String#new (inherited from Object) for allocating a new instance. You don’t call String#initialize (or #super) in your new instance so you got "" when you inspect the newly created object.

    BigDecimal defines method Bigdecimal#new, which allocates its own object. Object creation consists of two parts – allocating space for new object and initializing it. You only defined initializing new object, so you stay with default allocating space for object. If you want to override it, you should define #new in your new class and call BigDecimal‘s #new with proper arguments.

    Hope that clarifies a little what happen in your example.

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

Sidebar

Related Questions

I encountered a strange problem today. Whenever i put a breakpoint in one of
I encountered a, at least to my expectations, strange behavior in the binary serialization
I encountered the above error message after applying the OutputCache attribute on ActionResult methods
I encountered a strange problem where in I have developed 3 reports in SSRS
So I encountered a strange issue today - I had a simple creation of
I've encountered a strange bahaviour, when implementing a Facebook app using the iframe canvas
I recently encountered a strange problem: In Ruby 1.9, with the updated CSV library,
I have encountered a strange problem when using the File.Move command. The Programm actually
I've a 'C' program which has encountered a strange problem.. I'm getting segmentation fault
I'm looking over the Cloud Haskell package's Encoding.hs , and encountered some strange code

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.