My code vs solution code

Beginner here just trying to get some supplemental follow up info of the differences in code. My code just used a less than statement rather than the greater than equals and then used puts rather than return for the output. I tested it by adding a 4th check where I inverted hello and world and the output was still correct. Is there any benefit or draw back using either way to write the code?

My code:

def longer_string(str1, str2)
  if str1.length < str2.length
    puts str2
  else
    puts str1
  end
end

puts longer_string("app", "academy") # => "academy"
puts longer_string("summer", "fall") # => "summer"
puts longer_string("hello", "world")# => "hello"
puts longer_string("world", "hello")# => "world"

Solution code

def longer_string(str1, str2)
  if str1.length >= str2.length
    return str1
  else
    return str2
  end
end

puts longer_string("app", "academy") # => "academy"
puts longer_string("summer", "fall") # => "summer"
puts longer_string("hello", "world") # => "hello"

Well I found this article https://learn.co/lessons/puts-print-and-return-readme
it goes over the differences in puts, print, and return. I am just not totally sure how it applies to my code though lol

TL;DR

The main difference between your implementation is that you’re not actually returning the string, but only printing it to the screen. If the end goal is to return a value, then you should use the “return” keyword instead of puts.

LONG VERSION

The article you linked to explains that when you don’t use the keyword “return” followed by what you want to return, Ruby will the last thing that was evaluated. In your case, the last thing evaluated is a puts statement which evaluates to “nil.”

So, I wouldn’t say that your code is necessarily wrong, because it does what is intended, i.e., print the longer of the two strings; however, it’s not exactly what the “user” wanted. Since the user expects to be able to use “puts” with the method, we should assume that they want the longer string returned, and not just the longer string printed. In your solution the user’s use of the “puts” command doesn’t actually do anything. Try this with your code:

test = longer_string(“app”, “academy”) # => “academy”
puts("print the test variable after this line")
puts test

You might expect to get the longer string twice, once before the "print the test variable… " line and once after, or maybe you only expected to get it once after the “print the test variable…” line, but you only get it once before. This is because your code will always print the longer string whenever it is called, because your method specifically calls the “puts” command. It doesn’t print it twice because it doesn’t return the value of the longer string. So the user’s use of the puts command doesn’t actually print anything.

Instead of using “puts,” try using “p” instead. It’s yet another Ruby print command, but prints more information:

test = longer_string(“app”, “academy”) # => “academy”
puts("print the test variable after this line")
p test

Here, you’ll see that the last line prints “nil,” because that is what your code returned.

So, in the future, be cognizant of what the final outcome should be. If you simply what your method to print something and not return a value, then your code is perfect. If you want to be able to return a value, perhaps to store it in a variable for future use, then make sure you actually return the value using the “return” keyword and not “puts.”

1 Like

I just want to thank you for the response. I have never had anyone ever respond in such a detailed way to a question I have asked online. Your answer more than clarified my confusion on the puts vs return and the approach you mention of focusing on the final outcome is well noted. My further lessons dealing with loops and arrays I have been using that approach more and it really helps me write the code better by using a top, bottom, middle approach. Where I write the start, then the end of what I am trying to return, and then finally working to solve the problem in the middle. Makes the problem seem less challenging and stays more on task of what the “user” wants.

I did have one future question though that also confused me on this problem and that is why the answer would choose to use a >= rather than what I wrote as being only a < statement. To me it seems best to write code with the least amount of extra characters written for the desired response. The question if I am remembering correctly was saying something along the lines of “if the 2 strings are equal length then return str1.” So I understand the reason for the >= statement, but my answer directly addresses the one case str2 gets printed as well. Is there any pros or cons to either way or just personal preference at that point on how you want to write the code?

1 Like

Sorry for the looooong delay in replying. I had to put the course on hold for a while, but now have more time to work on it.

As for the ‘>=’ vs ‘<’ question. Either way is fine, in this scenario. I think the solution shows the ‘>=’ to make it follow the way they worded the problem (as you thought might be the case). There are times where one way is ‘better’ than another because it will make the code shorter (fewer elsif statements), more logical/easier to read and debug, more efficient, etc., but I think that in most cases either logic works, you’ll just have to make sure that your following code covers all the other scenarios correctly.