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 Calling Blocks

Dylan Barnard
Dylan Barnard
1,860 Points

Sample code doesn't run - missing a parameter

If you run the code as is you get a message like this:

block_method.rb:1:in `get_name': wrong number of arguments (0 for 1) (ArgumentError)  
        from block_method.rb:9:in `<main>'

You need a parameter for the get name method in the teacher's notes in order for the first procedure in order for it to run.

print prompt
...
...
get_name("Enter your name") do |your name|
....

As it currently is the prompt bit is missing.

2 Answers

Dylan Barnard
Dylan Barnard
1,860 Points

Hmm, yeah that could work as well. My aim though was to inform others that the supplied code in the notes and workspace wouldn't run, and show them one way of easily fixing it (the way that it's shown in the video around at 1:30). Here's the complete snippet with my fix for clarity.

def get_name(prompt, &block)
  print prompt
  name = gets.chomp
  block.call(name)
  name
end

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

puts "my_name: #{my_name}"
rdaniels
rdaniels
27,258 Points

I think what you are looking for is the following:

def get_name(prompt, &block)
  print prompt + ": "
  name = gets.chomp
  block.call(name)
  name
end

if you wanted the part under get-name I can show you that too...