Skip to content

Instantly share code, notes, and snippets.

@gerryster
Created April 25, 2016 17:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gerryster/99ca33575df594ff646faa9da6a8b5b8 to your computer and use it in GitHub Desktop.
Save gerryster/99ca33575df594ff646faa9da6a8b5b8 to your computer and use it in GitHub Desktop.
A demonstration of how Ruby's super keyword forwards on all arguments
# 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