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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:15:12+00:00 2026-06-03T11:15:12+00:00

I have some code that calculates the nth root of a number. Right now,

  • 0

I have some code that calculates the nth root of a number. Right now, that method only works on Fixnum, because I defined it inside the Fixnum class. It would be very easy to just do

class Float
    #same code as was in Fixnum
end

but that seems unneccessary. I have no idea how to dynamically call classes. I tried:

classes = [Fixnum, Float]
classes.each do |x|
    x.instance_eval do
        def root(pow)
            return self ** (1/pow.to_f)
        end
    end
end

but that did not work. How do I do this?
Note: After posting, I realized this might be a better fit for Programmers.SE, as it is theoretical, as well as single-problem based. Feel free to migrate accordingly…

  • 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-03T11:15:24+00:00Added an answer on June 3, 2026 at 11:15 am

    The relevant part of the class hierarchy looks like this:

    • Numeric
      • Integer
        • Older versions had Fixnum and Bignum as subclasses of Integer.
      • Float
      • Rational

    So patch your change into Numeric to cover them all at once:

    class Numeric
      def root(pow)
        return self ** (1/pow.to_f)
      end
    end
    

    Then you can do these things:

    >> 11.root(2) # Integer
    => 3.3166247903554
    >> 2.18.root(3) # Float
    => 1.296638256974172
    >> Rational(23, 42).root(6) # Rational
    => 0.9045094132598528
    >> 2**1000.root(42) # Integer
    => 2.2638347236157763
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a code that calculates heat transfer in some number of conductors. What
I have some code that shows results in a table Right now it will
So I have this code here, that (for now) just turns a base1 number
I have a code in MATLAB that calculates some values, prints graphs, and opens
I have some small piece of software that calculates the number of factors of
I now have to refactor some code, it's basically one method(its around 1000 lines
I have some code that calculates the lowest common multiple for a list of
I'm having a small issue, I have some code that calculates a mean value
I have some code here that calculates the maxValue of an array: public static
I have some code that will change the background color of a specific label

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.