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