Support

If you have a problem or need to report a bug please email : support@dsprobotics.com

There are 3 sections to this support area:

DOWNLOADS: access to product manuals, support files and drivers

HELP & INFORMATION: tutorials and example files for learning or finding pre-made modules for your projects

USER FORUMS: meet with other users and exchange ideas, you can also get help and assistance here

NEW REGISTRATIONS - please contact us if you wish to register on the forum

Users are reminded of the forum rules they sign up to which prohibits any activity that violates any laws including posting material covered by copyright

Scale Class [Ruby]

Post any examples or modules that you want to share here

Scale Class [Ruby]

Postby Urnsoft » Tue Feb 12, 2013 6:23 pm

Hi flows

A simple Ruby Class for scaling a number from a given range to specific range

EDIT :Sorry I removed it because its not working .read the post below.
I'm working on it

have a good day ;)
Last edited by Urnsoft on Wed Feb 13, 2013 2:47 am, edited 1 time in total.
Urnsoft
 
Posts: 15
Joined: Wed Aug 08, 2012 7:44 pm

Re: Scale Class [Ruby]

Postby trogluddite » Wed Feb 13, 2013 1:20 am

Hi there,
Nice to see my shiny red boxes getting some use - and thanks for the credit.

Sadly, I have some bad news for you - your class does not work; at least not the way intended. Attempting to scale '@a' just results in a "No Method" error.
It's a great idea though; converting one number range to another is such a common thing to need - and no problem with your maths.
It's cool to see someone else having a go at extending Ruby, and you're thinking along the right lines - so I'll go into a bit of detail about why it doesn't work

When you create a class you are defining a whole new type of object. The methods inside the class decide what you can (and cannot) do with one of those objects. So a "Door" class object might have "open" and "close" methods, but probably not "eat" and "drink". But defining "Door" as a class doesn't create a 'Door', it just tells Ruby how to make one. To actually get hold of one in the program code, you need to make a new one using "new".
Code: Select all
myFrontDoor = Door.new

The methods inside a class can only be used by an object of that class - so in your case a "Scale" object could use the method "convert between", but other types of object can't - which is why feeding in '@a' gives a "No Method" error - because it is 'Float' class object not a 'Scale' one.

So we could try making a new 'Scale' class object.
Code: Select all
@myScale = Scale.new
@myScale.scale_between(0,1,50,100)

But that doesn't help either - "Undefined method '-'". Because '@myScale' is a 'Scale' object, it has no maths methods, 'scale_between' is the only one defined for it, along with a few standard ones that every object gets. And if you look at the code - there's nowhere for the input number to be put in any more.

OK - if it is float numbers that we want to scale, how about defining '.scale_between' as a method of the 'Float' class; then it would be working with the right kind of objects. You can add new methods or modify old ones for a class any time you like by just putting a new "class XXX ... end" section in the code with the same name.
NEVER DO THIS!!
It is so very, very tempting to modify the built in classes, but they are shared everywhere in Ruby - including across multiple VST plugin exports. If you ever use modules shared around the forum, you just never know how your modifications might affect everyone else's programming, or whether their Kernel class mod's will affect yours. It's just a very bad habit to get into and is best avoided.

Really a class isn't what's needed. Rather than being a type of object, scaling is something we want to do TO objects - we want the 'scale_between' method to be something we can do to any number object that we choose.

And you can do that by changing only one word of code...
Rather than a class, you want a module. A module never makes new objects of its own, it is just a container for keeping definitions inside so that you can re-use them easily.

First, change "class Scale" to "module Scale"
This will throw an error - Ruby is just confused because it remembers old class definitions, and won't let you re-use the same name for a module. No problem - save file, close, and re-open FS to clear the memory.

Better still use "module Urn_Scale'
The same clashing of names between modules and classes can happen no matter where in a schematic you make them, and even between different schematics. 'Scale' is a common word and concept that other people might use in their own modules - so making the name unique by adding a personal tag will make clashes much less likely.
I've just recently modified all of my classes with the prefix "Trog::" for exactly this reason.

To use your new 'Scale' module, there are a couple of ways....

Attach the new method to each object when you need to.
You can add the 'scale_between' feature to any object you like by using the '.extend' method.
Code: Select all
@x = 10
@x.extend(Scale)
@a.scale_between(0,,200,500)

The 'Scale' module could have hundreds of methods, but you could add them all at once to any object this way. you just have to be careful that the type of object is appropriate for the methods. 'scale_between' won't work on strings, because the maths won't work on them, for example..

Use 'scale_between' as if it was just a normal method.
If you add an extra argument 'input_value', and then use 'input_value' instead of 'self' inside the method definition, then you get something like a normal FS method - there's no self, you just put all the values in as arguments.
And if you write the method to look like a regular FS one, then you can add it easily to any Ruby module that you choose.
You just use 'extend' again - but with no object this time, so that it is the Ruby primitive itself that gets extended.
Code: Select all
extend (Scale)

This is just the same as if you had written out the method in a normal run of code - but it saves you from having to type it over and over again when you want to use it in load of places. And you would use the method in just the same way as a 'bare' method, without any 'self'...
Code: Select all
scale_between(@a,0,1,100,250)
All schematics/modules I post are free for all to use - but a credit is always polite!
Don't stagnate, mutate to create!
User avatar
trogluddite
 
Posts: 1730
Joined: Fri Oct 22, 2010 12:46 am
Location: Yorkshire, UK

Re: Scale Class [Ruby]

Postby Urnsoft » Wed Feb 13, 2013 5:09 pm

Hi Trog

Many thanks for your informative post .

Well, it worked fine on my system so I thought I made something useful to share but after I saw your post I reopened the project and the same error ("No Method") showed up ,so I removed the attached file above to prevent any misleading issue.

Using modules instead of classes is a great idea
I added another argument for input value and change the module name to Urn_Scale also change the method name to scale_it , now it's working flawlessly :)

Scale module.fsm
(9.05 KiB) Downloaded 1468 times


are these header codes necessary for modules ? because I comment them out !
Code: Select all
#unless Module.const_defined?(:Urn_Scale)
#begin
#$Urn_scale_Instance = @this
#output 0,false


I'm just a Ruby nuby ,I do this like parrot :) ,lack of knowledge always leads me to malfunctioning problems but I'm lucky to have you guys here helping me to get rid of them .

cheers.
Urnsoft
 
Posts: 15
Joined: Wed Aug 08, 2012 7:44 pm

Re: Scale Class [Ruby]

Postby trogluddite » Wed Feb 13, 2013 8:12 pm

Urnsoft wrote:Many thanks for your informative post .

You're welcome - like I said, the idea is a very good one, and you are keen to learn, so I really don't mind helping out.

Urnsoft wrote:are these header codes necessary for modules ? because I comment them out

No they're not strictly necessary - they don't make a class/module work any better.

The reason for the "unless" part, is that you might load several plugins or modules that include the same red class module. In that case, the code would run every single time - but it would just be re-defining exactly the same things, and so slow down loading for no good reason. The "unless" just checks whether the same code has already run before and tells it not to bother if the class has already been defined.

The other bit with the "used" flag is totally cosmetic - in my own classes, there's some extra code that makes the title name light up in white if ever the class gets accessed. So it's just a way that you can see if the class has been used - so that you could delete any unused ones before doing your final export.

In fact, i would say they're all probably best deleted in your case. Unless there are dozens of lines of code,they're probably not all that useful - and for a short module they might waste more CPU than they save.

Urnsoft wrote:Well, it worked fine on my system

Don't worry, it's happened to me too. It's probably due to that same "Ruby memory" thing that I mentioned earlier.
When you define classes and modules, they stay defined for the whole editing session even if you delete the code.
So possibly Ruby had remembered some "prototype" code from when you were checking out the maths - and for some reason the other code was running, and hiding the fact that the class was broken.
It's not a massive problem - the memory always gets wiped if you close and re-open FS. But it's something to just have in the back of your mind if Ruby starts throwing a tantrum over the names that you want to use.

As a silly example - say you write this...
Code: Select all
Class MyKlass
   #some other code
end

There's now a class called "MyKlass". But, oops; I really wanted to call it "Fred", so I go in and edit it.
But 'MyKlass' is still remembered. In fact, not just 'MyKlass', but all of these...

MyKlass
MyKlas
MyKla
MyKl
MyK
My
M
F
Fr
Fre
Fred


Every time the name changed from one character of typing (or deleting), the whole class got redefined with a different name - and they're all still there, complete with all of the methods that were defined inside the class.
If i now want to make a module called 'MyK', I can't because Ruby thinks I already have a class of that name.
It has left me scratching my head many times before - especially when I have re-opened my 'broken' file and all of a sudden it works perfectly!
This isn't the fault of the FS developers. Ruby as a language just works that way - it is a side effect of the very powerful ways that Ruby allows us to manipulate Classes.

So if you ever see any kind of "Name Error" from Ruby and don't know why, just have a quick think what you edited recently - it could be that there's nothing wrong with your code at all, but Ruby just needs its memory wiping.
If you know that you are editing something that might be "sensitive", you can also use the little "on/off" button in the corner of the editor to stop the code parser until you know your edits are finished (I always forget to do this until it's too late!)
All schematics/modules I post are free for all to use - but a credit is always polite!
Don't stagnate, mutate to create!
User avatar
trogluddite
 
Posts: 1730
Joined: Fri Oct 22, 2010 12:46 am
Location: Yorkshire, UK

Re: Scale Class [Ruby]

Postby Urnsoft » Thu Feb 14, 2013 7:39 am

Every time the name changed from one character of typing (or deleting), the whole class got redefined with a different name - and they're all still there, complete with all of the methods that were defined inside the class.
If i now want to make a module called 'MyK', I can't because Ruby thinks I already have a class of that name.
It has left me scratching my head many times before - especially when I have re-opened my 'broken' file and all of a sudden it works perfectly!

it's the dark side of the ruby .thanks for clarifying .
If you know that you are editing something that might be "sensitive", you can also use the little "on/off" button in the corner of the editor to stop the code parser until you know your edits are finished (I always forget to do this until it's too late!)

I'll put the stickers around (Do Not!).

I added another method to it :
Code: Select all
scalelim_it(@a,0,10,50,100)

this will limit the incoming value into given range

Scale module II.fsm
(10.24 KiB) Downloaded 1431 times


:idea: Now I think of another module ,something like interactive min max detector for streaming values with lookahead option .well ,its little beyond my knowledge but no matter .
Urnsoft
 
Posts: 15
Joined: Wed Aug 08, 2012 7:44 pm


Return to User Examples

Who is online

Users browsing this forum: No registered users and 90 guests