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

Shawn Vogler
Shawn Vogler
564 Points

Hello, I'm having a tough time understanding methods. is it possible for a method to be a list of strings, like a poll?

When i was doing this little quiz, I was really tripped up thinking that hello had to output a string not necessarily a number. is it possible for a method to return a string? as in, the program asks you a question, then the user would input one of the answers? or maybe like a poll questionnaire?

Shawn Vogler
Shawn Vogler
564 Points

Follow up question, I don't understand what's wrong with this method:

def subtract(a, b)

 puts a-b

end

subtract (3, 2)

Is it something wrong with the puts part of code? sorry for these dumb questions...

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! I'm sort of the opinion that your second code shouldn't have passed. They never ask you to call the method, only define it. So when you properly called it in your first code, it failed. But when you improperly called it in your second code, it passed. The reason being is this: you don't get to decide what values go into that method. Treehouse is sending in data to make sure they get back the correct results.

But let's talk a little bit about what a method does. It's a reusable piece of code that you can run over and over again from elsewhere in your code. In this simple case, it takes two numbers and subtracts them and that doesn't seem like much. And, in all honesty, it isn't. But imagine for a second that you had a really tough calculation you had to do based on ten numbers, the user's geolocation, and the time of day. You can send all that into your method, have it run this really complex calculation for you, and then send back the result.

But the short answer to why your original code didn't work is that you called the method yourself. And Treehouse didn't ask for that. Note that in these challenges it's a good idea to try not to do anything they don't explicitly ask for. Even if your code functions outside the challenge, it might still cause the challenge to fail.

Hope this helps! :sparkles:

Shawn Vogler
Shawn Vogler
564 Points

Thank you for the answer. Much appreciated. I'm on a couple of challenges later on in this section now. It's asking me to return the remainder using the "%" using two arguments in a method. I'm pretty lost on this one. Here's what I've got so far:

def mod (a, b)

  puts "#{a} divided by #{b} = "

  puts a% / b%

  return (a%), (b%)

end

mod (10%), (2%)

Should I get rid of the unnecessary stuff like you were talking about earlier too?

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Shawn Vogler I'd say this as hint #1. Take a look closely at that challenge... does it ask you to print the string? or return it? :smiley: If it doesn't ask you to print anything... don't. Hint #2: do not call the method yourself. Treehouse will call it. If you're still stuck, post a new question to the forums with a link to the new challenge so we can keep the threads easier to search for other students :sparkles:

Shawn Vogler
Shawn Vogler
564 Points

Well this is how I changed my second question to get it right:

def subtract (a, b)

puts a - b

end

subtract (3), (2)

I don't really understand how that worked differently than the previous.