Best way to alias a function?

When live coding, it would be more convenient to do

VeryLongAndDescriptiveFunctionName parameter1, parameter2

like this:

v parameter1, parameter2

I can do

define :v do |parameter1, parameter2|
  VeryLongAndDescriptiveFunctionName parameter1, parameter2
end

but is there a shorter way? I might have hoped

alias v VeryLongAndDescriptiveFunctionName

would work, but it looks like alias won’t do what I want.

It’s of course a (very much unsupported) workaround, but here’s how you can do it:

@user_methods.alias_method :v, :VeryLongAndDescriptiveFunctionName

Other than that, it’s a matter of using nested functions as you mention above…

1 Like

A.K.A. an easter egg

Is that this, and are there others that I can look through?

It is this one:
https://ruby-doc.org/core-3.0.1/Module.html#method-i-alias_method

As for others? no idea :man_shrugging: I was purely going off Ruby’s core API, looking for methods that might do what you were after :slight_smile:

1 Like

Yes, I saw that, and it was one of the ones I couldn’t get to work because (as I now know) I didn’t know you had to put @user_methods. before alias_method. I guess I still don’t quite understand how all those classes are working in Sonic Pi, as I’ve only ever used that Ruby reference for array (and a few other other data types) methods.

Sure. We do funky things like a bunch of meta-programming in the Sonic Pi server code, and things will not happen in a standard way a lot of the time, as you can see.
Here’s where we begin setting up the language API/user methods:

1 Like

um, there’s one rabbit hole I’m better off not going down.

Hahaha! fair enough :slight_smile:

What is the particular benefit of doing that style of alias method @ethancrawford ?

Well, I suppose you could say it is shorter to type than the alternative of having to create an entirely new function to wrap your existing long named function…