Page 1 of 1

Ruby: convert array to list of objects?

Posted: Thu Jan 30, 2020 2:27 am
by deraudrl
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?

Re: Ruby: convert array to list of objects?

Posted: Thu Jan 30, 2020 2:44 am
by tulamide
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.

Re: Ruby: convert array to list of objects?

Posted: Thu Jan 30, 2020 2:57 am
by deraudrl
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.)

Re: Ruby: convert array to list of objects?

Posted: Thu Jan 30, 2020 8:04 pm
by trogluddite
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.

Re: Ruby: convert array to list of objects?

Posted: Thu Jan 30, 2020 8:37 pm
by deraudrl
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.

Re: Ruby: convert array to list of objects?

Posted: Fri Jan 31, 2020 1:18 am
by deraudrl
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 867 times

Hope it's of some use to somebody.

Re: Ruby: convert array to list of objects?

Posted: Fri Jan 31, 2020 4:12 am
by trogluddite
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: