title: How do I automatically cast a class instance to Float in Ruby?
body: <p>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:</p>
  
  <pre><code>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, &amp;block)
      if block_given?
        @angle.public_send(message, *args, &amp;block)
      else
        @angle.public_send(message, *args)
      end
    end
  end
  </code></pre>
  
  <p>It works fine. However when I try to use it with trig operations, e.g. Math.cos, I get:</p>
  
  <pre><code>&gt; a = Angle.new(0.0)
   =&gt; #&lt;Angle:0x00000000cdb220 @angle=0.0&gt; 
  @angle=0.0
  &gt; Math.cos(a)
  TypeError: can't convert Angle into Float
  </code></pre>
  
  <p>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?</p>