Created
April 25, 2016 17:55
-
-
Save gerryster/99ca33575df594ff646faa9da6a8b5b8 to your computer and use it in GitHub Desktop.
A demonstration of how Ruby's super keyword forwards on all arguments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A demonstration of how Ruby's super keyword forwards on all arguments: | |
class A | |
def foo(*args) | |
puts "super class args are #{args.inspect}, block_given?: #{block_given?}" | |
end | |
end | |
class B < A | |
def foo(*argsb) | |
puts "sub class args are #{argsb.inspect}, block_given?: #{block_given?}" | |
super # argsb and any passed in block will be forwarded onto the super class's foo method | |
end | |
end | |
b = B.new | |
b.foo(1, 2) { } | |
# Prints: | |
# sub class args are [1, 2], block_given?: true | |
# super class args are [1, 2], block_given?: true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment