Ruby Ruby Blocks Ruby Blocks Block Arguments

Should this code work for Ruby Blocks challenge?

Not sure I am understanding the premise of the question. I believe I have called the method get_name by assigning it to the variable "name". I also yielded the local variable name so it was able available out side of the get_name method. Is there a different convention that is expected"?

block_methods.rb
def get_name
  puts "Please enter your name: "
  name = gets.chomp
  yield name
  name
end

name = get_name do | my_name |
  puts "That's a cool name #{my_name}"
end

puts "My name: #{name}"

1 Answer

Jay McGavren
STAFF
Jay McGavren
Treehouse Teacher

The directions for the challenge say:

Call the method get_name with a block.

So you're actually not supposed to make any changes to the get_name method, you're just supposed to call it. When you call get_name, you need to pass a block. The block you pass needs to take a parameter. That parameter needs to be named: name. The challenge won't accept a block parameter named my_name.

It looks like you altered get_name to return a value and printed that (which is understandable, you were just trying to get the challenge to pass). But there's no need to do that; you can just print the value of the name parameter within the block.