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 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

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")
SY LOC
SY LOC
3,233 Points

Got it. Thank you!

No problem!