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 Basics (Retired) Ruby Methods Method Arguments: Part 1

trying to create a method called hello with an argument i am not typing the right code please help ```

i have to create a method named hello and it must contain an argument but i cant seem to type in the right code. i wrote: def hello (a) puts a+2 end

add (2)

method.rb
def hello (a)
  puts a + 2
end

add (2)

2 Answers

Cindy Lea
PLUS
Cindy Lea
Courses Plus Student 6,497 Points

You pretty much had it, but you added extra stuff they didnt ask for. The challenges are very picky. Heres what I used:

def hello (a) end

thank you for your help.

Without knowing exactly what the program is asking for -

If you want to just define a method hello with one argument it is

def hello(arg)
end

If you want to add the number 2 to your argument

def hello(arg)
  arg + 2
end

Additionally - you are calling the add method with the argument of 2, but you are defining the hello method, add is not defined, so in this case it should be hello(2). You need to remember to call the correct method with the arguments.

Note: puts will result in a 'nil' value. puts simply displays the result to stdout and RETURNS a value of 'nil'. If you want to RETURN the value of arg + 2 you can use the return keyword, or simply write arg + 2 as ruby will automatically return the value of the last expression. To state again - puts will only display the value in the command line. While return will return the value.

thank you for your help.