Skip to content

Instantly share code, notes, and snippets.

@AndrewO
Last active August 29, 2015 14:01
Show Gist options
  • Save AndrewO/9c93b250c5d887eb3885 to your computer and use it in GitHub Desktop.
Save AndrewO/9c93b250c5d887eb3885 to your computer and use it in GitHub Desktop.
class A
attr_reader :val
def initialize(a, &b)
@val = a || (b ? b.call : :nada)
end
end
class B < A
def initialize(b)
super(b)
end
end
# A.new(true).val
# => true
# A.new(false) { :ok }.val
# => :ok
# B.new(true).val
# => true
# B.new(false).val
# => :nada
# B.new(false) { :oops }.val
# => :oops
# How did that happen? `super` implicitly passes block params along even if you don't
class C < A
def initialize(c)
super(c, &nil)
end
end
# C.new(false) { :ok }.val
# => :nada
# RUBY_DESCRIPTION
# => "ruby 1.9.3p125 (2012-02-16 revision 34643) [x86_64-darwin10.8.0]"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment