I was trying to use RinRuby in my rails app... and there are two ways I can write the same code. 1) Using R.eval statements
R.eval "integrand <- function(q) {((-log(#{n}) + q*log(q) + (1-q)*log((1-q)/(#{n}-1)))^2)*(q^#{a}*(1-q)^#{b}*1/beta(#{a},#{b}))}"
R.eval "integ <- integrate(integrand, lower = 0, upper = 1)$val"
R.eval "eig <- (log(#{n}) - (#{b}/(#{a}+#{b}))*log(#{n}-1) - digamma(#{a}+#{b}+1) + (#{a}*digamma(#{a}+1)+#{b}*digamma(#{b}+1))/(#{a}+#{b}))"
R.eval "r_output <- (integ - eig)"
val = R.pull "r_output"
and 2)Using EOF wrap around the code
r = RinRuby.new(:echo => false)
r.eval <<-EOF
integrand <- function(q) {((-log(#{n}) + q*log(q) + (1-q)*log((1-q)/(#{n}-1)))^2)*(q^#{a}*(1-q)^#{b}*1/beta(#{a},#{b}))}
integ <- integrate(integrand, lower = 0, upper = 1)$val
eig <- (log(#{n}) - (#{b}/(#{a}+#{b}))*log(#{n}-1) - digamma(#{a}+#{b}+1) + (#{a}*digamma(#{a}+1)+#{b}*digamma(#{b}+1))/(#{a}+#{b}))
r_output <- (integ - eig)
EOF
val = r.pull "r_output"
Is one form expected to be faster than the other? I wanted to optimize my code so I thought of using the second one. The first form also echo's the RinRuby statements upon execution but they are turned off in second one. So I thought that would optimize the code but that is not the case, infact the second is taking more time ... can anyone help.