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

class array methodes expansion

Post any examples or modules that you want to share here

class array methodes expansion

Postby Nubeat7 » Sun Oct 20, 2013 8:53 pm

after tiffy and billv were posting some ruby tools and a lot of questions about array handling i was born the idea of an array methode expansion for easier array manipulation..

i added 9 new methodes to the array class which where asked in the forum or i needed already by my own, all methodes are available as nondestructive and destructive methodes..

for sure there are a lot of more useful methodes to add which are not already exist in the array class
Attachments
Array-Class-Methodes-Expansion.fsm
(5.32 KiB) Downloaded 542 times
Last edited by Nubeat7 on Wed Dec 19, 2018 5:17 pm, edited 12 times in total.
User avatar
Nubeat7
 
Posts: 1347
Joined: Sat Apr 14, 2012 9:59 am
Location: Vienna

Re: class array methodes expansion

Postby RJHollins » Sun Oct 20, 2013 10:49 pm

Oh this is going to be nice and handy !! Thanks NuBeat ! 8-)

I can think of a list of array handling within RUBY that would be great for toolbox.

What I don't know is whether they would fall under the 'Class' method.

For example: adding arrays, sorting, searching for item or items. Pretty much wringing out every possible need we'd have when dealing with arrays.

One idea is to make a RUBY code data base that would have linked arrays with wildcard searching, editing, copy/paste ... the gamut. :lol:

I'm sure there are some great, useful ideas out there.

Always appreciate your comments and contributions !!!

Big Thanks!
8-)
RJHollins
 
Posts: 1476
Joined: Thu Mar 08, 2012 7:58 pm

Re: class array methodes expansion

Postby billv » Sun Oct 20, 2013 10:58 pm

Thanks newbeat7... :)
BV MUSIC SYDNEY AUSTRALIA-Songwriting & Software Development.
http://users.tpg.com.au/billv
billv
 
Posts: 970
Joined: Tue Aug 31, 2010 3:34 pm
Location: Australia

Re: class array methodes expansion

Postby Nubeat7 » Mon Oct 21, 2013 8:20 am

RJHollins wrote:For example: adding arrays, sorting, searching for item or items. Pretty much wringing out every possible need we'd have when dealing with arrays.

..

lot of this stuff is already handled in the arrayclass, stuff like join, shift, sort, map ....
it is well documented here:
http://ruby-doc.org/core-2.0.0/Array.html

the idea is to write useful methodes which are not here already, often they are combinations of existing methodes or just a map function with a specified block but also some specific sorts could be of interest or some string handling..

i also thought about putting prefixes for specific methodes like "str_array_methode_name" instead just "array_methode_name" when it is a methode to handle strings, i think some kind of norm would be a good idea here.

it is also about to not occupy every possible methodename...
User avatar
Nubeat7
 
Posts: 1347
Joined: Sat Apr 14, 2012 9:59 am
Location: Vienna

Re: class array methodes expansion

Postby Nubeat7 » Mon Oct 21, 2013 9:10 am

found a mistake in the set_to_range methode, forgot the braces...
note that this methode only works right for 0..1 values!

any body have an idea how to make it work for all values?

reuploaded the file in the first post
User avatar
Nubeat7
 
Posts: 1347
Joined: Sat Apr 14, 2012 9:59 am
Location: Vienna

Re: class array methodes expansion

Postby Nubeat7 » Mon Oct 21, 2013 8:00 pm

ok looks that i found a working solution which works with all +/- combinations too..

i renamed the "set_to_range" methode to "scale_values" because i think the name makes mor clear what it does,
but the methode has 4 arguments now:

scale_values(newMin,newMax,oldMin,oldMax)

so you need to tell the methode also the old range but the old Range default is set to 0..1
so if this is the case (old range 0..1) only the new range is needed in the arguments!

some prooving would be appreciated

and i also found an easy implementation to use also the "<", ">", "<=", ">=" operators for array comparing

updated version in first post
User avatar
Nubeat7
 
Posts: 1347
Joined: Sat Apr 14, 2012 9:59 am
Location: Vienna

Re: class array methodes expansion

Postby trogluddite » Mon Oct 21, 2013 10:21 pm

Couple of quick comments...

You don't need the 'random_order' method - there's already 'Array.shuffle' which does that.
I've done it myself loads of times; writing a load of code, and then seeing it right there the next time I look at the API doc's! He he - sometimes there's just too much Ruby! :lol:

For the scale_values, you can use the general form...
new_val = (old_val - old_min) * (new_max - new_min) / (old_max - old_min) + new_min
...that will work even for the 'reverse' situations, so you wont need all the 'abs' and 'if' methods.
For an even bigger speed boost, the maths can be re-factored and a couple of constant values pre-calculated...
Code: Select all
scale = (new_max - new_min) / (old_max - old_min)
offset = new_min - (old_min * scale)
new_array = old_array.map{|x| x * scale + offset}

That way the bit inside the loop loses the greedy divide and all the subtractions.

Keep up the good work - it's really nice to see some "Ruby toolkits" coming through! :D
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: 1706
Joined: Fri Oct 22, 2010 12:46 am
Location: Yorkshire, UK

Re: class array methodes expansion

Postby Nubeat7 » Mon Oct 21, 2013 11:18 pm

trogluddite wrote:You don't need the 'random_order' method - there's already 'Array.shuffle' which does that.
I've done it myself loads of times; writing a load of code, and then seeing it right there the next time I look at the API doc's!


:D oh, yes thats cool! good that i postet the link to the api by myself :lol: not so bad in this case becaus i found the code in the web and put it in without asking myself a lot
trogluddite wrote:For the scale_values, you can use the general form...
new_val = (old_val - old_min) * (new_max - new_min) / (old_max - old_min) + new_min
...that will work even for the 'reverse' situations, so you wont need all the 'abs' and 'if' methods.
For an even bigger speed boost, the maths can be re-factored and a couple of constant values pre-calculated...
CODE: SELECT ALL
scale = (new_max - new_min) / (old_max - old_min)
offset = new_min - (old_min * scale)
new_array = old_array.map{|x| x * scale + offset}

That way the bit inside the loop loses the greedy divide and all the subtractions.

Keep up the good work - it's really nice to see some "Ruby toolkits" coming through!


great! thanks a lot for that, just dropped it in and also deleted the random_order..

updated version in first post
User avatar
Nubeat7
 
Posts: 1347
Joined: Sat Apr 14, 2012 9:59 am
Location: Vienna

Re: class array methodes expansion

Postby Nubeat7 » Wed Dec 19, 2018 5:18 pm

after there are some troubles with FS Guru site i uploaded this one here in the forum again. last version in first post!
User avatar
Nubeat7
 
Posts: 1347
Joined: Sat Apr 14, 2012 9:59 am
Location: Vienna


Return to User Examples

Who is online

Users browsing this forum: No registered users and 16 guests