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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:51:37+00:00 2026-05-27T09:51:37+00:00

I have an Angle class that I want to behave like a float, with

  • 0

I have an Angle class that I want to behave like a float, with additional behavior. I have created the class to contain a float and proxy all unknown methods to it:

class Angle
  include Math
  def initialize(angle=0.0)
    @angle = Float(angle)

    # Normalize the angle
    @angle = @angle.modulo(PI*2)
  end

  def to_f
    @angle.to_f
  end

  # Other functionality...

  def method_missing(message, *args, &block)
    if block_given?
      @angle.public_send(message, *args, &block)
    else
      @angle.public_send(message, *args)
    end
  end
end

It works fine. However when I try to use it with trig operations, e.g. Math.cos, I get:

> a = Angle.new(0.0)
 => #<Angle:0x00000000cdb220 @angle=0.0> 
@angle=0.0
> Math.cos(a)
TypeError: can't convert Angle into Float

I know I can use Float(a) to convert to a float, but it’s inconvenient since I want this class to behave like a float. Is there a way to automatically convert Angle to float in these cases?

  • 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-27T09:51:37+00:00Added an answer on May 27, 2026 at 9:51 am

    Looking at the implementation of Math.cos, you can see it calls a macro called Need_Float, which then calls a function rb_to_float. Line 2441 of rb_to_float checks to see if the object passed in is of type Numeric. So it seems the only way to have your own class act as a float in the Math family of functions is to have it inherit from Numeric or a descendant of Numeric. Thus, this modification of your code works as expected:

    class Angle < Numeric
      include Math
      def initialize(angle=0.0)
        @angle = Float(angle)
    
        # Normalize the angle
        @angle = @angle.modulo(PI*2)
      end
    
      def to_f
        @angle.to_f
      end
    
      # Other functionality...
    
      def method_missing(message, *args, &block)
        if block_given?
          @angle.public_send(message, *args, &block)
        else
          @angle.public_send(message, *args)
        end
      end
    end
    
    if __FILE__ == $0
      a = Angle.new(0.0)
      p Math.cos(a)
    end
    

    I’m not sure what side effects inheriting from Numeric will have, but unfortunately this looks like the only way to have your code work the way you want it to.

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

Sidebar

Related Questions

I have Angle class that I want initialized to a random value. The Angle
I have created a set of templates that I want to share across multiple
I have some shapes created by class Rectangle and I want to surround them
I have a method that, given an angle for North and an angle for
I have created a collision class to detect collisions using pixels. In my class
I have a class that takes standard address properties and stores them. The State
I have a class template similar to the one that follows below that is
In my Android application I have created an SVG image converter class. It parses
I have a page that looks like: <div id=header>...</div><div id=navigation>...</div> similar for body and
I have a square that rotates to a random angle and then travels in

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.