Please consider the following code. I got it to work but by trial and error. I don’t really understand why the commands are where they are. I only understand the last - that shows the where the code ends. Can somebody explain the other s please.
# Write a method that takes in a string. Return the longest word in # the string. You may assume that the string contains only letters and # spaces. # # You may use the String `split` method to aid you in your quest. # def longest_word(sentence) v=sentence.split(" ") lw=nil i=0 while i<v.length crW=v[i] if lw==nil lw=crW elsif crW.length>lw.length lw=crW end #THIS IS THE PART WHERE I DON'T GET i+=1 end return lw end # These are tests to check that your code is working. After writing # your solution, they should all print true. puts("\nTests for #longest_word") puts("===============================================") puts( 'longest_word("short longest") == "longest": ' + (longest_word('short longest') == 'longest').to_s ) puts( 'longest_word("one") == "one": ' + (longest_word('one') == 'one').to_s ) puts( 'longest_word("abc def abcde") == "abcde": ' + (longest_word('abc def abcde') == 'abcde').to_s ) puts("===============================================")