Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
Ruby gives us different ways to call blocks. In this video, we'll explore the yield
keyword and using block.call
.
Code Samples
This is an example of calling the block using the call
method:
def get_name(prompt, &block)
print prompt
name = gets.chomp
block.call(name)
name
end
my_name = get_name do |your_name|
puts "That's a cool name, #{your_name}!"
end
puts "my_name: #{my_name}"
The block_given?
keyword can be used to conditionally call a block if one was passed to a method:
def get_name(prompt, &block)
print prompt
name = gets.chomp
block.call(name) if block_given?
name
end
my_name = get_name("Name: ") do |your_name|
puts "That's a cool name, #{your_name}!"
end
puts "my_name: #{my_name}"
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up