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 Foundations Methods Instance Methods

SY LOC
SY LOC
3,233 Points

placement

curious if someone could explain how this method works. 'hello("James", "Mike The Frog")' always have to be at the end.

def hello(name1, name2)
  puts "Hello #{name1}"
  puts "Hello #{name2}"
end
hello("James", "Mike The Frog")

If I put 'hello("James", "Mike The Frog")' after the first line, it wouldn't work. Why is that?

def hello(name1, name2)
hello("James", "Mike The Frog")
  puts "Hello #{name1}"
  puts "Hello #{name2}"
end

1 Answer

lordcozycat
lordcozycat
11,940 Points

With

hello("James", "Mike The Frog")

you're calling the hello method. The reason is doesn't work in your second piece of code is because it has no place inside the definition of your method.

So:

# Start to define the hello method, setting it to accept input variables.
def hello(name1, name2)
# Define what happens when the hello method is called with two variables, name1 and name2.
  puts "Hello #{name1}"
  puts "Hello #{name2}"
end
# Ends the definition. After this, other code is no longer a part of the hello method.

# Calls the hello method with the two variables, and looks inside for what to do, which is to puts "Hello <name>" to each variable.
hello("James", "Mike The Frog")