Skip to content

Instantly share code, notes, and snippets.

@armw4
Created January 24, 2014 20:13
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 armw4/8605358 to your computer and use it in GitHub Desktop.
Save armw4/8605358 to your computer and use it in GitHub Desktop.
I wanted to know if you could arbitrarily pass a block to an overridden method via "super". It seems like an obvious "yes", but my curiosity got the better of me once more. Normally you call "super" with no arguments. Don't recall ever seeing it invoked with a block. So there you have it...
class Base
def deploy(&block)
puts "work"
yield if block_given?
puts "done working"
end
end
class Derived < Base
def deploy
puts "overridden"
super
end
end
class Derived2 < Base
def deploy
puts "overridden with block"
super { puts "hiya" }
end
end
Derived.new.deploy # => overridden
# work
# done working
Derived2.new.deploy # => overridden with block
# work
# hiya
# done working
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment