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

Ruby: convert array to list of objects?

For general discussion related FlowStone

Ruby: convert array to list of objects?

Postby deraudrl » Thu Jan 30, 2020 2:27 am

What I'm trying to do is a simple Ruby component that has a variable number of inputs and behaves like a C 'vsprintf' function. I know I can do something like "output 0,sprintf(@ins[0],@ins[1],...)", where @ins[0] is the format string.

But I'd like a way to not have to change the Ruby code itself every time I use it, just add inputs as needed. (Obviously the format string needs to match the inputs, but it's being passed in as well.)

I was hoping something like "output 0,sprintf(@ins.each)" would work, but I'm getting wrapped around the axle trying to pass the elements of the input array into sprintf as separate arguments. Some help please?
I keep a pair of oven mitts next to my computer so I don't get a concussion from slapping my forehead while I'm reading the responses to my questions.
deraudrl
 
Posts: 239
Joined: Thu Nov 28, 2019 9:12 pm
Location: SoCal

Re: Ruby: convert array to list of objects?

Postby tulamide » Thu Jan 30, 2020 2:44 am

Did you try the splat operator?

pass
Code: Select all
*@ins


For example:
Code: Select all
def something(a, b, c, d)
#some code dealing with the 4 arguments
end

something(*@ins) #will pass @ins[0] to a, @ins[1] to b, etc.
"There lies the dog buried" (German saying translated literally)
tulamide
 
Posts: 2687
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: Ruby: convert array to list of objects?

Postby deraudrl » Thu Jan 30, 2020 2:57 am

Ah, perfect, thanks: "output 0, sprintf(*@ins)", does exactly what I wanted. You can put just about anything you want in the front, and it comes out the back as formatted text.

On a vaguely related note...I know how to add inputs and outputs to an existing Ruby component, but how do you remove them? (I always seem to insert one more than I need.)
I keep a pair of oven mitts next to my computer so I don't get a concussion from slapping my forehead while I'm reading the responses to my questions.
deraudrl
 
Posts: 239
Joined: Thu Nov 28, 2019 9:12 pm
Location: SoCal

Re: Ruby: convert array to list of objects?

Postby trogluddite » Thu Jan 30, 2020 8:04 pm

deraudrl wrote:On a vaguely related note...I know how to add inputs and outputs to an existing Ruby component, but how do you remove them?

It's in the bottom block of icons on the R-Click context menu for a connector, after all of the connector-type selectors:
+ = Add a new input immediately below the one clicked.
X = Delete the clicked input.
N = Rename the clicked input.
arrows = Move the clicked input up or down the connector order.

NB) Here's the Ruby API documentation for the sprintf method. Depending what languages you're used to, there may be a few minor differences and useful additions. One that I particularly like is that Ruby allows positional parameters, where you can specify which of the input arguments gets used for each field by index (%n$...), including using the same argument multiple times:
Code: Select all
sprintf("%3$s %1$s %2$03d %1$s", "one", 2, "three")   #=> "three one 002 one"

Also worth noting is that Ruby isn't as fussy about the arguments as many languages, as it will apply its standard conversions to them before substitution. So a string field (%s) will print the standard String conversion for almost any object, and numeric fields convert between float and integer automatically.
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: Ruby: convert array to list of objects?

Postby deraudrl » Thu Jan 30, 2020 8:37 pm

trogluddite wrote:
deraudrl wrote:On a vaguely related note...I know how to add inputs and outputs to an existing Ruby component, but how do you remove them?

It's in the bottom block of icons on the R-Click context menu for a connector, after all of the connector-type selectors:
+ = Add a new input immediately below the one clicked.
X = Delete the clicked input.
N = Rename the clicked input.
arrows = Move the clicked input up or down the connector order.
File that under, "If it had been a snake, it would have bitten me." (forehead slap)

trogluddite wrote:Depending what languages you're used to, there may be a few minor differences and useful additions. One that I particularly like is that Ruby allows positional parameters, where you can specify which of the input arguments gets used for each field by index (%n$...), including using the same argument multiple times...
Also worth noting is that Ruby isn't as fussy about the arguments as many languages, as it will apply its standard conversions to them before substitution. So a string field (%s) will print the standard String conversion for almost any object, and numeric fields convert between float and integer automatically.
Very cool. My background is mostly straight C (dating back to the early '80s), some C++ since I retired, plus the usual suspects (Fortran, Algol, Basic) in the distant past, and assembly dialects for more obscure hardware than I care to remember. But the newer interpreted and Web-based stuff, not so much.
I keep a pair of oven mitts next to my computer so I don't get a concussion from slapping my forehead while I'm reading the responses to my questions.
deraudrl
 
Posts: 239
Joined: Thu Nov 28, 2019 9:12 pm
Location: SoCal

Re: Ruby: convert array to list of objects?

Postby deraudrl » Fri Jan 31, 2020 1:18 am

What prompted this whole thing was this thread: viewtopic.php?f=3&t=37064
where I saw:
trogluddite wrote:I was also very taken by your dynamic tooltips which show the current settings of some of the modules. In all my years using SM/FS, I don't think I've ever seen anyone do that before - very handy for debugging, and one of those ideas which seems so simple and obvious the moment you've seen someone else do it first!

Took me forever to find an actual example...in the meantime, I came up with this:
Tooltip Debug.fsm
(4.2 KiB) Downloaded 752 times

Hope it's of some use to somebody.
I keep a pair of oven mitts next to my computer so I don't get a concussion from slapping my forehead while I'm reading the responses to my questions.
deraudrl
 
Posts: 239
Joined: Thu Nov 28, 2019 9:12 pm
Location: SoCal

Re: Ruby: convert array to list of objects?

Postby trogluddite » Fri Jan 31, 2020 4:12 am

deraudrl wrote:Took me forever to find an actual example...in the meantime, I came up with this:

That's pretty neat! When it comes to diddling with text, it's pretty amazing what you can do with a single line of Ruby!

One suggestion I'd make is to enclose the formatting parts within a "purgable" module. Purgable modules are removed automatically when exporting to VST or executable, so are handy for "debug build" bits and bobs. Just R-click a module and select 'Purgable' to toggle it; the module border goes stripy when it's enabled. Remember not to wrap the tooltip primitive itself in there, though, otherwise it won't show for the parent module!

When the tip is just a short literal string, I don't think it's worth bothering about, and the 'tooltip' primitive is probably purgable by default. But in this case, there's no sense having the Ruby formatting strings within an export, where there the tooltips will never be seen.

deraudrl wrote:"If it had been a snake, it would have bitten me." (forehead slap)

I've never known anything other than being a boffin with all the common-sense of a rock! I try to convince myself that I'd miss out on many wonderful things in life if I didn't always have to take the road less-travelled; but it's not much consolation when my best impression of belonging to a social species slips into the uncanny valley yet again! :lol:
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


Return to General

Who is online

Users browsing this forum: Google [Bot], Majestic-12 [Bot] and 25 guests