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

前端 未结 7 1083
面向向阳花
面向向阳花 2021-02-10 06:19

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:58

    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
    

提交回复
热议问题