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

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,599 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