#! /usr/bin/ruby

# --- passing/use of a code block through Proc.new ---

# if passed a code block, process may or may not choose to run it
#    - in this case it runs the block iff a block is actually passed
#      AND x and y are both integers
def process(x, y)
    if (!x.kind_of?(Integer)) then
       puts "Error: #{x} is not an integer"
    elsif (!y.kind_of?(Integer)) then
       puts "Error: #{y} is not an integer"
    elsif (!block_given?) then
       puts "Error: no block passed to run"
    else
       result = eval Proc.new.call
    end
end


# test with a user-defined block of code and some parameter values

puts "enter the custom block of code you want process(x,y) to run on its parameters"
puts "   e.g. x/y"
block = gets.chomp

puts "Enter the value to use for parameter x"
xParam = gets.chomp.to_i

puts "Enter the value to use for parameter y"
yParam = gets.chomp.to_i

result = process(xParam,yParam) { block }
puts "Result is #{result}"

# -------------- Sample Run -------------------
#
# enter the custom block of code you want process(x,y) to run on its parameters 
#    e.g. x/y
# x/y+x
# Enter the value to use for parameter x
# 15
# Enter the value to use for parameter y
# 4
# Result is 18

