#! /usr/bin/ruby

# --- runtime customization of functions through yields and eval ---

# process expects to be passed three variables and a yield block,
#    and performs pre and post processing around the block
def process(a, b, c)

    # do any default pre-processing here
    puts "processing #{a}, #{b}, #{c}"

    # now use the yield for any customized processing
    result = eval yield

    # do any default post-processing here
    puts "result is #{result}"
end

# get a block from the user
puts "enter the custom block of code you want executed on parameters a,b,c"
puts "   e.g. a.to_i + b.to_f * c.to_i"
block = gets.chomp

# get user values for the three parameters from the user
puts "enter value for x"
x = gets.chomp
puts "enter value for y"
y = gets.chomp
puts "enter value for z"
z = gets.chomp

# run the customized function
result = process(x, y, z) { block }

# -------------- Sample Run -------------------
#
# enter the custom block of code you want executed on parameters a,b,c
#    e.g. a.to_i + b.to_f * c.to_i
# a.to_i + b.to_f + c.to_i
# enter value for x
# 1
# enter value for y
# 2
# enter value for z
# 4
# processing 1, 2, 4
# result is 7.0

