ruby on rails - Writing a method that simply multiplies the object by something -


i'm trying convert bunch of numbers imperial metric on front end of site depending on if user has set measurement_units 'metric' or 'imperial'

i can @myweight*.45 convert number, want write helper method this

def is_imperial?   if user.measurement_units == 'metric'     *0.453592   elsif user.measurement_units == 'imperial'     *1   end end 

then able this: @myweight*.is_imperial?

i'm not sure how assign *value method is_imperial?

thanks help!

edit:

@myweight float calculated adding several numbers.

i'm trying find elegant way of converting number shows on site metric if user has metric value in measurement_units field on user model.

i assumed need create helper method in application_helper.rb. not correct?

if want measurement_units method dynamic based on user, think need make instance method.

modify is_imperial? method return right number:

def is_imperial?   if measurement_units == 'metric'     0.453592   elsif measurement_units == 'imperial'     1   end end 

then can call method this:

@myweight.send(:*, is_imperial?) 

if @myweight represents user object might have change this:

@myweight.weight.send(:*, is_imperial?) 

methods end ? in ruby expected return true or false, should rename method weight_conversion_factor.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -