Practical use for undef

Possible duplicate:
undef - Why do you want to uninstall a method in ruby?

Can anyone lay out a practical use for undef in ruby? I come from languages ​​like javascript and python that do not have built-in. Of course, you can imitate it in a language such as javascript:

var obj = { func:function(){alert("works")} } obj.func() // -> "works" delete(obj["func"]) obj.func() //->obj.func() is not a function 

but I never used to do this. Are there any general situations where undef is really useful?

EDIT - Just came across this in the Ruby Programming Language book:

Another way to prevent objects from copying is to use undef to simply remove the cloning and duplication methods. Yet another approach is to override clone and duplicate so that they throw an exception with an error message that specifically states that copies are not allowed. Such an error message may be useful for programmers who use your class.

+4
source share
2 answers

I can only say that I never used this after 5 years. Perhaps more useful, grepping across the entire source of the rails shows only 7 instances, all of which are related to meta-programming. (undef_method has more instances in 22). It is apparently useful for testing the behavior of classes with and without a module.

Hopefully someone can point out some more traditional uses.

EDIT See the previous question: undef - Why do you want to define a method in ruby?

+6
source

Blocking a child class from calling a method defined in a superclass (this is actually used in mongrel setup.rb ) (rephrasing):

 class Item def set do_something end end class ExecItem < Item undef set end 

Although I think undef_method will have the same effect.

+6
source

Source: https://habr.com/ru/post/1335620/


All Articles