diff --git a/Ruby_Hooks/slides.html b/Ruby_Hooks/slides.html index 1e3a369..4dfe38a 100644 --- a/Ruby_Hooks/slides.html +++ b/Ruby_Hooks/slides.html @@ -508,11 +508,13 @@ * This is how you define a class method --- it's a singleton method on the class itself * You will probably find few good reasons to call `remove_method` or `undef_method` * See https://ruby-doc.org/core-2.3.0/Module.html#method-i-undef_method for docs + * You can also call `undef method_name` instead of `undef_method :method_name` * Removing a method will allow any superclass methods to still be called * Only example I can think of is if you dynamically added it * Undefining a method will set the method to return a `NoMethodError` * Only example I can think of is if you're trying to look like an older Ruby + --- class: strict_conversion diff --git a/Ruby_Hooks/slides.md b/Ruby_Hooks/slides.md index 57540d2..dcab1fe 100644 --- a/Ruby_Hooks/slides.md +++ b/Ruby_Hooks/slides.md @@ -373,11 +373,13 @@ A subclassed in B * This is how you define a class method --- it's a singleton method on the class itself * You will probably find few good reasons to call `remove_method` or `undef_method` * See https://ruby-doc.org/core-2.3.0/Module.html#method-i-undef_method for docs + * You can also call `undef method_name` instead of `undef_method :method_name` * Removing a method will allow any superclass methods to still be called * Only example I can think of is if you dynamically added it * Undefining a method will set the method to return a `NoMethodError` * Only example I can think of is if you're trying to look like an older Ruby + --- class: strict_conversion diff --git a/Ruby_Hooks/undef.rb b/Ruby_Hooks/undef.rb new file mode 100644 index 0000000..18e59d2 --- /dev/null +++ b/Ruby_Hooks/undef.rb @@ -0,0 +1,21 @@ +class A + def a + 1 + end +end + +class B < A + def a + 2 + end +end + +puts B.new.a +# => 2 + +class B + undef a +end + +puts B.new.a +