Page 1 of 1

class array methodes expansion

PostPosted: Sun Oct 20, 2013 8:53 pm
by Nubeat7
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

Re: class array methodes expansion

PostPosted: Sun Oct 20, 2013 10:49 pm
by RJHollins
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-)

Re: class array methodes expansion

PostPosted: Sun Oct 20, 2013 10:58 pm
by billv
Thanks newbeat7... :)

Re: class array methodes expansion

PostPosted: Mon Oct 21, 2013 8:20 am
by Nubeat7
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...

Re: class array methodes expansion

PostPosted: Mon Oct 21, 2013 9:10 am
by Nubeat7
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

Re: class array methodes expansion

PostPosted: Mon Oct 21, 2013 8:00 pm
by Nubeat7
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

Re: class array methodes expansion

PostPosted: Mon Oct 21, 2013 10:21 pm
by trogluddite
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

Re: class array methodes expansion

PostPosted: Mon Oct 21, 2013 11:18 pm
by Nubeat7
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

Re: class array methodes expansion

PostPosted: Wed Dec 19, 2018 5:18 pm
by Nubeat7
after there are some troubles with FS Guru site i uploaded this one here in the forum again. last version in first post!