I have a Ruby on Rails application and I need to duplicate some computations in both Ruby and JavaScript. Ruby is used in the server side but I also need to compute a students grade on the browser using JavaScript.
My first thought is to build up a JavaScript function using strings, convert it to JSON, ship it to the browser where it is decoded and used as a normal JavaScript function. Does this sound workable to you? I've tried something simple like
def letterGradeCalc "function calcLetterGrade(score) { if( score >= 90 ) { return 'A'; } else if( score >= 80 ) { return 'B'; } else if( score >= 0 ) { return 'F'; } else return ''; }".to_json end
but it is not a valid JavaScript function when the browser gets it. It kinda looks like one but it has double quotes around it.
Am I barking up the wrong tree here? I get the feeling that there is some insanely easy way to do this and but I'm completely missing it. :)