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

Jillian Dunleavy
Jillian Dunleavy
3,685 Points

Would someone help me with this ruby block objective?

Call the method get_name with a block. Assign the block argument a variable called name. Do at least one thing in the block such as printing out the name.

I've tried a few variations that relate to what I had in the workspace during the video, but I can't get it.

block_methods.rb
def get_name
  name = "My Name"
  name = gets.chomp
  yield name
end

name - get name do
  puts #{name}
end

2 Answers

Jeff Wilkey
Jeff Wilkey
10,296 Points

You're kind of close you should be closer to something more along the lines of this:

def get_name
  name = "Jillian"
  yield name
end

# Write your code here
get_name do |name|
  puts "#{name}"
end
Jeff Wilkey
Jeff Wilkey
10,296 Points

I suggest trying to watch the video again to understand the syntax here a bit better.

Jesse Zelaya
Jesse Zelaya
13,539 Points

Try this:

def get_name
  print "Enter your name: "
  name = gets.chomp
  yield name
  name
end

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