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] aliasing a method name without copying the method?

For general discussion related FlowStone

[Ruby] aliasing a method name without copying the method?

Postby tulamide » Sat Oct 03, 2020 12:05 am

Is it possible?

Code: Select all
def foo
  bar
end
alias_method :foo, :tulamide

This will create a copy, so I now have foo and tulamide.

Of course I can do
Code: Select all
def foo
  bar
end
def tulamide
  foo
end


But what I'm really looking for is a way to call foo directly using a different name. Any chance?
"There lies the dog buried" (German saying translated literally)
tulamide
 
Posts: 2686
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: [Ruby] aliasing a method name without copying the method

Postby trogluddite » Sat Oct 03, 2020 2:59 pm

No, you can't make an alias which points to the same UnboundMethod ("method definition") object. In fact, all attempts to manipulate a method definition will result in a copy being made - for example, the below code will display two different object IDs for two unique UnboundMethod objects...
Code: Select all
watch "test1", RubyEdit.instance_method(:watch).__id__
watch "test2", RubyEdit.instance_method(:watch).__id__

...so there's no chance of a sneaky workaround by using "define_method" or other meta-programming tricks.


So, either you have to suffer the copy or, if you need the alias to reflect changes to the original, some kind of forwarding (your 2nd example being the simplest and most direct form of that).
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: 1727
Joined: Fri Oct 22, 2010 12:46 am
Location: Yorkshire, UK

Re: [Ruby] aliasing a method name without copying the method

Postby tulamide » Sat Oct 03, 2020 9:58 pm

Thanks Trog, for the confirmation.

This topic is something that is a bit annoying about Ruby. When I use something called "alias" I don't expect it to actually copy it. And the other way around: When you DO want to copy (a class), you have to be careful, because although the two methods are called dup and clone, one of them doesn't actually make a full copy, but more of a template copy.

Very unintuitive.
"There lies the dog buried" (German saying translated literally)
tulamide
 
Posts: 2686
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: [Ruby] aliasing a method name without copying the method

Postby trogluddite » Sun Oct 04, 2020 12:55 am

tulamide wrote:Very unintuitive.

It sure is! It's one of the bits of Ruby that most often catches me out (especially when objects are nested).

Even once the usual Ruby rules for copying are understood, there's another issue - many Ruby add-ons, including FlowStone's custom classes, don't obey the rules anyway!!

The base "Object" class has default definitions for "dup" and "clone" which work fine for most user-defined classes. But these assume that all of an object's state is stored in Ruby objects assigned to instance variables, and that's not the case for "C-extension" objects which encapsulate non-Ruby data - such as, for example, handles to GDI+ objects!

To see what I mean, try "dupping" or "cloning" some Ruby GUI objects (e.g. Brushes, Pens, GraphicsPaths etc.). The copying methods themselves won't complain, but when you try to use the returned copies, they either don't work or will crash FlowStone (on v3.0.6 at least - I should test this in the Alphas!).

To be fair, I don't recall anyone else complaining that they've run into this problem. However, all FS custom classes are kind_of?(Object), and "dup" and "clone" are part of the interface of "Object" - so in OO terms, it's very naughty not to implement them properly! When copying is really not possible, raising a "NotImplementedError" is a valid, documented option - but returning a broken object which might later crash the interpreter is not!!
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: 1727
Joined: Fri Oct 22, 2010 12:46 am
Location: Yorkshire, UK

Re: [Ruby] aliasing a method name without copying the method

Postby tulamide » Sun Oct 04, 2020 6:58 am

trogluddite wrote:When copying is really not possible, raising a "NotImplementedError" is a valid, documented option - but returning a broken object which might later crash the interpreter is not!!

Indeed!
And both, dup and clone, are often not usable. You brought up a very dangerous example. A not dangerous, but annoying one is, when I needed to copy the nodes in the Spline class. Those are nested. I learned about dup and clone the hard way, and ended up programming my own copy method that creates a true deep copy by creating a new node class instance and then filling it by calling itself for each layer. A lot of work!

At least we can program our own workarounds.
"There lies the dog buried" (German saying translated literally)
tulamide
 
Posts: 2686
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany


Return to General

Who is online

Users browsing this forum: No registered users and 53 guests

cron