Skip to content

Instantly share code, notes, and snippets.

@tjaved573
Last active May 12, 2022 18:58
Show Gist options
  • Save tjaved573/7fb5ed949590c710f6b456399be85063 to your computer and use it in GitHub Desktop.
Save tjaved573/7fb5ed949590c710f6b456399be85063 to your computer and use it in GitHub Desktop.
Ruby-Yield Statement
# 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