Ruby nuby needs help

For general discussion related FlowStone
Post Reply
strangeChild
Posts: 47
Joined: Sat Apr 27, 2013 8:04 pm

Ruby nuby needs help

Post 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?
strangeChild
Posts: 47
Joined: Sat Apr 27, 2013 8:04 pm

Re: Ruby nuby needs help

Post 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. :?
User avatar
chackl
Posts: 233
Joined: Tue Aug 17, 2010 8:46 pm
Location: Austria / Salzburg

Re: Ruby nuby needs help

Post 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
100% accuracy is the guarantee to your success. The value alters if you combine it with musical creativity.
strangeChild
Posts: 47
Joined: Sat Apr 27, 2013 8:04 pm

Re: Ruby nuby needs help

Post 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.
Post Reply