Lesson 17/19 - Methods with arguements

Im stuck with what to do on this lesson and would appreciate some help!

Here is my code:

def welcome
gets “name”
return “Hello, #{name}”
end

welcome

You need to set an argument for welcome

Example: def welcome(name)

the get “name” = you are making a string, when you should be using the argument inside the method.

Does this make sense?

I still do not understand what you’re trying to explain. I have been trying to grasp this concept on my own without any luck. Was wondering if I could take a look at someone else’ code so I can learn from it

When you define a method and you set the method name like this: def method

You also need to put an argument in () to the right of the method name like this: def method(argument)

Next, you use the argument within the method. You don’t use the method name within the method.

Here is your code:

def welcome
gets "name"
return "Hello, #{name}"
end

welcome

  1. def welcome needs an argument. ex. def welcome(argument)

  2. When you say gets “name” but you have not defined what name is. You need to make name a variable first
    like this outside of the method:

puts “What is your name?”
name = gets.chomp #this is the name variable and it
#equals what ever the user inputs (because gets is waiting for input from user)

  1. Once you’ve made name a variable you use #{name} in the method like you are already doing.

  2. Also by using double quotes like this: gets “name”
    You’ve made name a String which is just text and doesn’t have a function.

Does this make more sense?

This makes alot more sense now. I have reworked my code and it looks much better as it was before. Seems like I was just overthinking alot.

But I still made a mistake with my code because it says: Oops, try again. It looks like your welcome method doesn’t include the person’s name in its output.

Here is my refined code:

def welcome(x)
puts "What is your name?"
name = gets.chomp
return "Hello, #{name}"
end

welcome(1)

You are on the right track but…

The problem is you are over thinking this whole problem. I went back and looked at the problem. You should reset your code and start over. I don’t wanna out right give you the answer, otherwise you wont learn it. You will learn from the struggle of finding the answer.

I can clarify things though, which will help guide you, but my advice would be to re-read the whole problem again and start over.

1 Like

I agree that you shouldn’t share the code off the bat but the only reason that I am posting here is because I really do not have any idea. I am slow when it comes to learning code so it takes awhile for me to grasp a concept. As for this lesson, I have honestly tried my best to solve everything without any luck. It’d be a great pleasure if you could give me a couple more hints instead of saying go do it yourself.

I appreciate your honest response. I will explain to you since it has been some time.

def welcome(x)
puts "What is your name?"
name = gets.chomp
return "Hello, #{name}"
end

welcome(1)

Your code above doesnt answer the question because the problem is only looking for you to create a method and use the argument inside the method.

This is what the question is stating:

1. Change your welcome method definition. Add a single string parameter called name.
2. Remove your puts statement and replace it with return "Hello, #{name}"

In number 1. it is saying add a argument called name like this: def method(name)
When you define a method, to the right of the name goes the argument in ( ). A methods argument is kinda like the method having a variable unique to itself. With the argument , you use this within the method.

In number 2. it is telling you to remove the puts “statement” and replace puts with return "Hello, #{name}"
When you take the argument and put it within #{ } it will replace what ever the name was defined as, just like if it were a variable.

Does this make sense? let me know if I have to explain anything further.

This is what the final code should look like:

def welcome(name)
return "Hello, #{name}"
end

4 Likes

Wow, I cant believe that was all that it took to solve that question! I was thinking about this problem last night and dreamed about it too! You were of great help. I appreciate your clarification and sticking through in order to resolve my problem :smiley:

1 Like

Anytime! Your welcome!

It can be tricky at times and I often find myself going, “Really!?” but the more you code and the further you go, you will get quicker and faster in your understanding.

1 Like

def welcome(name)
return “Hello, #{name}”
end

welcome(“juan”)