## Tuesday, October 10, 2017

### Define a function with inline Ruby code in QSoas

QSoas can read and execute Ruby code directly, while reading command files, or even at the command prompt. For that, just write plain Ruby code inside a ruby...ruby end block. Probably the most useful possibility is to define elaborated functions directly from within QSoas, or, preferable, from within a script; this is an alternative to defining a function in a completely separated Ruby-only file using ruby-run. For instance, you can define a function for plain Michaelis-Menten kinetics with a file containing:

ruby
def my_func(x, vm, km)
return vm/(1 + km/x)
end
ruby end


This defines the function my_func with three parameters, $x$, $v_m$ (vm) and $K_m$(km), with the formula: $\frac{v_m}{1 + K_m/x}$

You can then test that the function has been correctly defined running for instance:

QSoas> eval my_func(1.0,1.0,1.0)
=> 0.5
QSoas> eval my_func(1e4,1.0,1.0)
=> 0.999900009999


This yields the correct answer: the first command evaluates the function with x = 1.0, vm = 1.0 and km = 1.0. For $x = K_m$, the result is $v_m/2$ (here 0.5). For $x \gg K_m$, the result is almost $v_m$. You can use the newly defined my_func in any place you would use any ruby code, such as in the optional argument to generate-buffer, or for arbitrary fits:

QSoas> generate-buffer 0 10 my_func(x,3.0,0.6)
QSoas> fit-arb my_func(x,vm,km)


To redefine my_func, just run the ruby code again with a new definition, such as:
ruby
def my_func(x, vm, km)
return vm/(1 + km/x**2)
end
ruby end

The previous version is just erased, and all new uses of my_func will refer to your new definition.

#### See for yourself

The code for this example can be found there. Browse the qsoas-goodies github repository for more goodies !