Skip to content

Instantly share code, notes, and snippets.

@kaochenlong
Last active January 30, 2016 10:18
Show Gist options
  • Save kaochenlong/28b4c3b0ebc7b51c5706 to your computer and use it in GitHub Desktop.
Save kaochenlong/28b4c3b0ebc7b51c5706 to your computer and use it in GitHub Desktop.
Ruby block demo
greeting_and_do_something = Proc.new do |message|
puts "Hello, #{message}"
yield(message) if block_given?
puts "wish you have a good time"
message
end
# callback demo
def trainingWithCompletion(&block)
puts "training begin..."
result = block.call(123)
puts "training result = #{result}"
puts "training complete..."
end
puts "*" * 50
trainingWithCompletion { |x|
puts "--------------------------------"
puts "you just pass #{x} in the block"
puts "--------------------------------"
x * 2 + 100
}
puts "*" * 50
trainingWithCompletion(&greeting_and_do_something)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment