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
Ruby arrays with basic math
12 posts
• Page 1 of 2 • 1, 2
Ruby arrays with basic math
Hi,
Not once you have to push each index in the array into some math operation to generate from it some new values. The greenies are quite limited in that case, so it leaves us with Ruby as an irreplaceable solution.
So let's say we have a simple array like
1
2
3
And we want to push each index through (x*2)+1 to get
3
5
7
How does Ruby do it? By the way, I think those math tricks should get their own category in the default factory toolbox
Not once you have to push each index in the array into some math operation to generate from it some new values. The greenies are quite limited in that case, so it leaves us with Ruby as an irreplaceable solution.
So let's say we have a simple array like
1
2
3
And we want to push each index through (x*2)+1 to get
3
5
7
How does Ruby do it? By the way, I think those math tricks should get their own category in the default factory toolbox
-
kortezzzz - Posts: 763
- Joined: Tue Mar 19, 2013 4:21 pm
Re: Ruby arrays with basic math
As said a million times, ruby-doc.org is your best friend!
It is explaining each and every class, like the array class. If you have a look there you'll find the method "map".
You will notice two things. First are those {} brackets. They enclose a block. If you want to know what a block is ruby-doc.org is your friend.
The second is the exclamation mark after map. It instructs Ruby to use map inline. If you want to know more about the !, well, you now know who your friend is
https://ruby-doc.org/core-1.9.3/Array.html
It is explaining each and every class, like the array class. If you have a look there you'll find the method "map".
- Code: Select all
a = [1, 2, 3]
a.map! { |n| n * 2 + 1 }
You will notice two things. First are those {} brackets. They enclose a block. If you want to know what a block is ruby-doc.org is your friend.
The second is the exclamation mark after map. It instructs Ruby to use map inline. If you want to know more about the !, well, you now know who your friend is
https://ruby-doc.org/core-1.9.3/Array.html
"There lies the dog buried" (German saying translated literally)
- tulamide
- Posts: 2714
- Joined: Sat Jun 21, 2014 2:48 pm
- Location: Germany
Re: Ruby arrays with basic math
Thanks tulamide,
My problem with that site is that stuff there not always fit FS's Ruby practical syntax. Therefore, Your explanations here worth much than a gold
However, I'll try to look up there in the future before posting, but posting anyway has it's own advantage: When someone sees the topic, he may get a new idea out of blue just because it mentioned and say to himself "wait a moment, I can use that there and there and solve some complexities in my schematic..."
It happened to me many times So, I hope that this post will have the same effect for any newbie that will ever join us in the future. Cheers!
My problem with that site is that stuff there not always fit FS's Ruby practical syntax. Therefore, Your explanations here worth much than a gold
However, I'll try to look up there in the future before posting, but posting anyway has it's own advantage: When someone sees the topic, he may get a new idea out of blue just because it mentioned and say to himself "wait a moment, I can use that there and there and solve some complexities in my schematic..."
It happened to me many times So, I hope that this post will have the same effect for any newbie that will ever join us in the future. Cheers!
-
kortezzzz - Posts: 763
- Joined: Tue Mar 19, 2013 4:21 pm
Re: Ruby arrays with basic math
kortezzzz wrote:My problem with that site is that stuff there not always fit FS's Ruby practical syntax.
I do have some sympathy with this, though I would put it the other way around - DSPr have made their Ruby API, and coded all of their examples, so that they are not idiomatic Ruby.
Ruby coders do not omit the parentheses around method arguments, generally speaking - it WILL bite you in the ass at some point when the parser finds an ambiguity. They do not use single-letter variable names (no working coder has done that since the 80's, and only then because of language restrictions!) They use lower_snake_case for their method names, not CamelCaseThatLooksLikeACeeFunctionOrAClassName. Just overriding Kernel::puts to forward to watch would have made a lot of existing internet examples work without having to tinker with the code.
If you're going to say to people; "here's our API, now go learn the rest of Ruby yourself", it would help if what you've already taught them fits the usual working practice of Ruby coders.
All schematics/modules I post are free for all to use - but a credit is always polite!
Don't stagnate, mutate to create!
Don't stagnate, mutate to create!
-
trogluddite - Posts: 1730
- Joined: Fri Oct 22, 2010 12:46 am
- Location: Yorkshire, UK
Re: Ruby arrays with basic math
kortezzzz wrote:Thanks tulamide,
My problem with that site is that stuff there not always fit FS's Ruby practical syntax. Therefore, Your explanations here worth much than a gold
However, I'll try to look up there in the future before posting, but posting anyway has it's own advantage: When someone sees the topic, he may get a new idea out of blue just because it mentioned and say to himself "wait a moment, I can use that there and there and solve some complexities in my schematic..."
It happened to me many times So, I hope that this post will have the same effect for any newbie that will ever join us in the future. Cheers!
Just happened for me with your question/post !
Thanks ... I would not have known about this.
- RJHollins
- Posts: 1571
- Joined: Thu Mar 08, 2012 7:58 pm
Re: Ruby arrays with basic math
RJHollins wrote: Just happened for me with your question/post !
Yep, it happens a lot. Moreover, sometimes people just feel too embarrassed to ask anything that would tag them as newbies, so they will sit and wait until someone else will ask. It happens not only on forums, but also schools, colleges, professional courses or wherever people afraid to loose their "cool" image.
trogluddite wrote: DSPr have made their Ruby API, and coded all of their examples, so that they are not idiomatic Ruby.
Trog,
sometimes setting-up a working code in FS's Ruby requires you writing it accurately (including the correct method at the start and the correct number of "end"s in the end). Sites never mention that, so you should know how to formulate a complete code to use an example method, or you end up with a "Ruby candy". For those who don't know the basics, that would be a serious problem, because reading those 2 lines of example codes while the complete code is actually 6 lines, will lead them to nowhere. It happened to me a lot.
-
kortezzzz - Posts: 763
- Joined: Tue Mar 19, 2013 4:21 pm
Re: Ruby arrays with basic math
I mildly disagree as a generalization. When I started with Ruby, I had no idea about it. In fact, I didn't even know it existed, until it came to Flowstone and you tested it.If you're going to say to people; "here's our API, now go learn the rest of Ruby yourself", it would help if what you've already taught them fits the usual working practice of Ruby coders.
I taught it all myself. And the one companion that I could trust from day 1 is ruby-doc.org! It'S the other way 'round, just a few examples, like the console, are non-helpful. The vast majority of the core is. And more importantly, you soon learn that it's consistent in the coding itself (very poorly described, sorry). Just like the nature of the language (object-orientation, inheriting), you only need to learn a new thing once. As soon as you know how a class is built, you can access any class in Ruby. As soon as you know how a block works, you have access to thousands (? well, but nearly) of methods.
A few years before Ruby I started with Python. I never got as far as with Ruby, and it took me so much longer to learn what I know about Python (which isn't much anymore). And let's not even start with C++
But Ruby is so welcoming and with the RubyEdits and direct access to views in Flowstone it is even more comfortable than the "full" Ruby.
Btw., trog, the spline class was created just 2 weeks or so after I started with Ruby. I didn't know of the style guide at that time, but other than that, I think it proves the accessabilty of Ruby!
"There lies the dog buried" (German saying translated literally)
- tulamide
- Posts: 2714
- Joined: Sat Jun 21, 2014 2:48 pm
- Location: Germany
Re: Ruby arrays with basic math
I do get tired of loops though. Is'nt there a primitive method of rewrites like for preset files or .dll's?
As Robust as that probably is; I'm up to the learning curve. It seems something like that would be worth learning. I love ruby, but too many different instances working at the same time is bad news. Rewriting globally through a primitive seems like a great idea. I'd tried it by using string split and string find and it lags out. I'd obviously looked and saw there was another method but I'd had problems tracking down a good instruction of how to use that.
Can anyone point Me to the right resource please?
As Robust as that probably is; I'm up to the learning curve. It seems something like that would be worth learning. I love ruby, but too many different instances working at the same time is bad news. Rewriting globally through a primitive seems like a great idea. I'd tried it by using string split and string find and it lags out. I'd obviously looked and saw there was another method but I'd had problems tracking down a good instruction of how to use that.
Can anyone point Me to the right resource please?
-
wlangfor@uoguelph.ca - Posts: 912
- Joined: Tue Apr 03, 2018 5:50 pm
- Location: North Bay, Ontario, Canada
Re: Ruby arrays with basic math
wlangfor@uoguelph.ca wrote: I'd tried it by using string split and string find and it lags out. I'd obviously looked and saw there was another method but I'd had problems tracking down a good instruction of how to use that.
Can anyone point Me to the right resource please?
You better upload a small schematic that shows what you've done so far and what results you are trying to achieve. It would be much easier for the experts to answer.
-
kortezzzz - Posts: 763
- Joined: Tue Mar 19, 2013 4:21 pm
Re: Ruby arrays with basic math
Yeah, I remember seeing an example - I'll track it down and then people can comment more effectively.
-
wlangfor@uoguelph.ca - Posts: 912
- Joined: Tue Apr 03, 2018 5:50 pm
- Location: North Bay, Ontario, Canada
12 posts
• Page 1 of 2 • 1, 2
Who is online
Users browsing this forum: No registered users and 80 guests