Return to Snippet

Revision: 38244
at December 29, 2010 14:29 by Shamaoke


Initial Code
# 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!
]

Initial URL

                                

Initial Description
Call a block if it's given otherwise call a method.

Initial Title
Call a block or a method

Initial Tags
ruby

Initial Language
Ruby