def takyBlock(p1) if block_given? yield(p1) else p1 end end takeBlock("no block") takeBlock("no block") {|s| s.sub(/no /, '')}
class TaxCalculator def initialize(name, &block) @name, @block = name, block end def getTax(amount) "#@name on #{amount} = #{ @block.call(amount) }" end end tc = TaxCalculator.new("Sales tax") {|amt| amt * 0.075 } tc.getTax(100) tc.getTax(250)