Created
May 25, 2018 17:54
-
-
Save havenwood/3ca22b3e03a16509708002d6c3c13562 to your computer and use it in GitHub Desktop.
Example's of how Ruby's super keyword passes on args and blocks
This file contains hidden or 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
class Foo | |
def foo arg = false | |
{arg: arg, block: block_given?} | |
end | |
end | |
class Bar < Foo | |
def foo *args | |
super | |
end | |
end | |
Bar.new.foo(true) { } | |
#=> {:arg=>true, :block=>true} | |
class Qux < Foo | |
def foo *args | |
super() | |
end | |
end | |
Qux.new.foo(true) { } | |
#=> {:arg=>false, :block=>true} | |
class Bongo < Foo | |
def foo *args | |
super *args, &nil | |
end | |
end | |
Bongo.new.foo(true) { } | |
#=> {:arg=>true, :block=>false} | |
class Wombat < Foo | |
def foo *args | |
super &nil | |
end | |
end | |
Wombat.new.foo(true) { } | |
#=> {:arg=>false, :block=>false} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment