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