Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 334
Ben Crouse Avatar asked Nov 05 '08 21:11

Ben Crouse


2 Answers

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.

like image 119
dennisjbell Avatar answered Sep 21 '22 20:09

dennisjbell


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.

like image 40
Kent Fredric Avatar answered Sep 25 '22 20:09

Kent Fredric