Last active
May 12, 2022 18:58
-
-
Save tjaved573/7fb5ed949590c710f6b456399be85063 to your computer and use it in GitHub Desktop.
Ruby-Yield Statement
This file contains hidden or 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
# Ruby Yield Statement | |
# Yield statement is used for a method that takes in a block and needs to invoke the code block. | |
# In this e.g, name() method takes in a block defined in line #10. | |
# In the method definition, wherever 'yield' keyword comes up, the code block is rendered. | |
def name | |
puts "A yield will be called with id of 12" | |
yield 12 | |
puts "A yield will be called with id of 14" | |
yield 14 | |
end | |
name {|i| puts "I am called by yield name #{i}"} | |
#// Output: | |
#// A yield will be called with id of 12 | |
#// I am called by yield name 12 | |
#// A yield will be called with id of 14 | |
#// I am called by yield name 14 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment