Skip to content

Commit

Permalink
Ruby Hooks: Verify that undef m is same as undefine_method :m
Browse files Browse the repository at this point in the history
  • Loading branch information
booch committed Nov 10, 2016
1 parent 4d042d5 commit 84225a2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Ruby_Hooks/slides.html
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions Ruby_Hooks/slides.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 21 additions & 0 deletions Ruby_Hooks/undef.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 84225a2

Please sign in to comment.