Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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"?

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

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

puts "My name: #{name}"

1 Answer

Jay McGavren
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.