Skip to content

Latest commit

 

History

History
29 lines (21 loc) · 364 Bytes

yield.md

File metadata and controls

29 lines (21 loc) · 364 Bytes

Yield

Yield is a keyword that allows you to execute a block of code passed to a method.

def hello
  puts "Hello " + yield
end

hello do
  "David"
end

# returns "Hello David"

Yield can also be passed arguments:

def hello(arg)
  puts "Hello " + yield(arg)
end

hello("Dear") do |arg|
  "#{arg} David"
end

# returns "Hello Dear David"