Skip to content

Instantly share code, notes, and snippets.

@havenwood
Created May 25, 2018 17:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save havenwood/3ca22b3e03a16509708002d6c3c13562 to your computer and use it in GitHub Desktop.
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
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