Project

General

Profile

Feature #4910 ยป pro.rb

Example file with old and new variants - rklemme (Robert Klemme), 06/20/2011 08:50 PM

 

class Class
alias call new

def to_proc(*args)
lambda {|*a| new(*args)}
end
end

class Hlp1
def initialize(factory)
@factory = factory
end

def process(x)
op = @factory.call
op.process x
end
end

class Hlp2
def initialize(&factory)
@factory = factory
end

def process(x)
op = @factory.call
op.process x
end
end

# funny name:
class Professor
def process(o)
printf "%p: Processing %p...\n", self, o
end
end

# call[]
puts 'old style'
hlp = Hlp1.new lambda { Professor.new }
hlp.process "foo"
hlp.process "bar"

puts 'with feature'
hlp = Hlp1.new Professor
hlp.process "foo"
hlp.process "bar"

# &callback
puts 'old style'
hlp = Hlp2.new do Professor.new end
hlp.process "foo"
hlp.process "bar"

puts 'with feature'
hlp = Hlp2.new &Professor
hlp.process "foo"
hlp.process "bar"

    (1-1/1)