Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.