Where is the best place to add methods to the Integer class in Rails?

前端 未结 7 2203
死守一世寂寞
死守一世寂寞 2021-02-10 05:56

Where is the best place to add a method to the integer class in Rails? I\'d like to add a to_meters and to_miles methods.

7条回答
  •  北恋
    北恋 (楼主)
    2021-02-10 06:53

    Why not just:

    class Feet
      def self.in_miles(feet)
        feet/5280
      end
    end
    

    usage:

    Feet.in_miles 2313
    

    Or maybe look at it the other way:

    class Miles
      def self.from_feet(feet)
        feet/5280
      end
    end
    
    Miles.from_feet 2313
    

提交回复
热议问题