Page 1 of 1

Ruby nuby needs help

Posted: Tue Oct 22, 2013 4:41 am
by strangeChild
I wrote a Ruby module to replace some green math just to learn a bit... after discovering 1/12 = 0 but 1.0/12 = 0.083333333 things went more or less as expected.

But when I reopened the schematic it was flagging a Ruby error... I couldn't figure it out as it looked OK and had been working when I saved... then I discovered if it clicked on each of the inputs it would resolve the error...

Here's the code the module contains:

Code: Select all

def _Nx12thRoot2Inv(x)
   1.0/2**(x*(1.0/12))
end


scaleLength = (@fbWidth - @nutThickness - 0.5*@fretWidth )
scaleLength = scaleLength * (1/(1 - _Nx12thRoot2Inv(@Nfrets)))

output scaleLength * (1 - _Nx12thRoot2Inv(@fretNumber)) + @xOffset + @nutThickness
(It's for the fret x offsets on a guitar scale display.)
Can anyone sort me out on this?

Re: Ruby nuby needs help

Posted: Tue Oct 22, 2013 5:57 am
by strangeChild
The file is stable now... and I didn't keep the version where this happened and can no longer replicate.

I believe it had something to do with the fact that I had saved some invalid code and then deleted it and saved again... after priming each input with a trigger the code saved properly. :?

Re: Ruby nuby needs help

Posted: Fri Oct 25, 2013 10:15 am
by chackl
Hey - This is not strange - you may take a look to the definitions of Float and Integer ;)

12 is interpreted as Integer
12.0 is interpreted as Float


For example:
1/12 means
Integer / Integer = Integer - An integer is an Integer so no dos will be stored

If you write:
1.0/12.0 means:
Float = Float/Float and you will get your expected Value


So ruby takes always the highes Level object in a line and converts it - This means:
1/12.0
Integers / Float = Float

1.0/12
Float / Integer = Float

If you want to convert values of a Variable:

Code: Select all

@var1=1
@var2=12

@var1 / @var2
=> 0

@var1.to_f / @var2
=> 0.08333333333333333


Regards C.Hackl

Re: Ruby nuby needs help

Posted: Sat Oct 26, 2013 5:22 pm
by strangeChild
Yeah... once I figured out what part of my code didn't work I knew it was a typecast issue.

That's the downside of not having to declare variable types.

Has anyone else experienced problems saving files after a Ruby module with an error was deleted... that was my real problem here.