views:

506

answers:

6

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.

+2  A: 

Normally (and logically), integers can't be converted to miles or to meters. It sounds like you may want to create a new class like "Feet" or "inches" that is initialized with an integer, then contains methods like size_in_miles or size_in_meters. For convenience those methods could return decimal or float types, but you might also want to write a miles class or a meters class.

As an alternate method, you might want to create a static method in your new class that would have a signature like this:

Float feetToMiles(integer I)

that you would call

miles = Feet.feetToMiles(5280);

and get miles = 1.0

Zak
+1  A: 

Create your own module/library which you include into scope when you need it to perform this task.

Such as "requre 'unitCoversions' "

And Chances are, somebody has already done this if you look hard enough :)

However DONT try modifying the native core class, that will only end in Misery.

( Also, the class you want to extend is 'numeric' , that will apply to both Integers and Floats :) )

Not entirely clear why I shouldn't do this... Rails does this to the string class to great success.

Because it can be done doesn't mean it should be done. 'Monkey Patching' as it is known can have all sorts of odd side effects, and they can be an epic failure when done wrong.

Do it when there is no good alternative.

Because if you really wanted to do something daft, you could build an entire framework that ALL it did was monkey patch the core classes.

Just for example, flip databasing on its head.

5.getArtist(); 
10.getEvent(); 
100.getTrack();

etc etc. there is no limit to how many bad ways there are to do that.

"Bob".createUser();

misery in a cup.

If you want to do something practical, have a Convert class or function,

convert( 3 , { :from=>:miles, :to=>:meters });

at least you're not polluting the global namespace and core functions that way and it makes more coherent sense.

Kent Fredric
Not entirely clear why I shouldn't do this... Rails does this to the string class to great success.
Ben
A: 

If you were going to do this, which you shouldn't, then you would put your code into:

config/initializers/add_methods_that_are_naughty_to_numeric.rb

Rails would automatically run these for you.

jonnii
+2  A: 

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
Cameron Booth
+2  A: 

I agree monkey patching should be used with care, but occasionally it just make sense. I really like the helpers that allow you to type 5.days.ago which are part of the active_support library

So some of the other answers might be better in this case, but if you are extending ruby classes we keep all our extensions in lib/extensions/class_name.rb

this way when working on a project it is quick and easy to find and see anything that might be out of the ordinary with standard classes.

danmayer
+5  A: 

If you have your heart set on mucking with the Numeric (or integer, etc) class to get unit conversion, then at least do it logically and with some real value.

First, create a Unit class that stores the unit type (meters,feet, cubits, etc.) and the value on creation. Then add a bunch of methods to Numeric that correspond to the valid values unit can have: these methods will return a Unit object with it's type recorded as the method name. The Unit class would support a bunch of to_* methods that would convert to another unit type with the corresponding unit value. That way, you can do the following command:

>> x = 47.feet.to_meters
=> 14.3256
>> x.inspect
=> #<Unit 0xb795efb8 @value=14.3256, @type=:meter>

The best way to handle it would probably be a matrix of conversion types and expressions in the Unit class, then use method_missing to check if a given type can be converted to another type. In the numeric class, use method_missing to ask Unit if it supports the given method as a unit type, and if so, return a unit object of the requested type using the numeric as its value. You could then support adding units and conversions at runtime by adding a register_type and register_conversion class method to Unit that extended the conversion matrix and Numeric would "automagically" pick up the ability.

As for where to put it, create a lib/units.rb file, which would also contain the monkey_patch to Numeric, then initialize it in config/environment.rb bu requiring the lib/units.rb file.

dennisjbell
The only thing I'd suggest doing differently is doing the initialize using the initializers directory instead of in environment.rb. Great post.
jonnii