Last active
January 30, 2016 10:18
-
-
Save kaochenlong/28b4c3b0ebc7b51c5706 to your computer and use it in GitHub Desktop.
Ruby block demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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