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

Daniel Wise
Daniel Wise
9,119 Points

Having troubles with this array project

This part of a lab I have been working on is requiring us to create a method that returns a string "Hello, my name is #{name}." and then pass that method into an array in another method. Here is what I have made so far.

def badge_maker(name)
     name = "Arel"
     say_hello = "Hello, my name is #{name}."
     return say_hello 
end 

def batch_badge_creator(attendies)
    attendies = ["Edsger", "Ada", "Charles", "Alan", "Grace", "Linus","Matz"]
    attendies.each do 
        return "Hello, my name is #{attendies}"
    end 
end

What I cannot figure out is how to get the batch_badge_creator to print out the names with the "Hello, my name is #{name}" string to each name (it should print out a list saying Hello my name is Edsger, Hello my name is Ada, ect). This should be using the badge_maker method above. Hoping to get some help for this one.

1 Answer

Tim Knight
Tim Knight
28,888 Points

Daniel,

There's a few things you should be considering here:

  1. If you have a method parameter, you'd be setting that value when you execute the method, you don't need to set it again inside. Here's what I mean...

You have

def badge_maker(name)
    ...
end

So you execute that you'd just call badge_maker("Arel")

  1. A method can only return one thing. So your second method doing a look of returns isn't going to work, but it will only return the first return.

Here's what I would say... your badge_maker method should take an array then create your array of names and loop through that using puts.

So given this code, what might your method for badge_maker look like?

attendees = ["Edsger", "Ada", "Charles", "Alan", "Grace", "Linus","Matz"]
badge_maker(attendees)