/ Published in: Ruby
Call a block if it's given otherwise call a method.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
# encoding: utf-8 class Parent def one 'ok!' end end class Child < Parent def initialize(&block) @do = block || method(:one) end def do @do.call end end child_one = Child.new child_two = Child.new { 'ok two!' } puts [ child_one.do, #=> ok! child_two.do #=> ok two! ]