A little more help before my interview

I was redoing the last problem Same Char Collapse to prepare for my interview and came across something that I found odd and could use some insight on.

Here is my code

def same_char_collapse(str)
collapse_switch = true
on = true
off = false

while collapse_switch
chars = str.split("")
collapse_switch = off

chars.each.with_index do |char, i|     
  if chars[i] == chars[i + 1]
     chars[i] = ""
     chars[i + 1] = ""
     collapse_switch = on

new_str = chars.join("")


return new_str

puts same_char_collapse(“zzzxaaxy”) #=> “zy”

because zzzxaaxy -> zxaaxy -> zxxy -> zy

puts same_char_collapse(“uqrssrqvtt”) #=> “uv”

because uqrssrqvtt -> uqrrqvtt -> uqqvtt -> uvtt -> uv

(aside from the “on” “off” variables that I wrote in for my own personal benefit it is nearly identical to the instructor code. However it kept shooting back “undefined”. After looking at the solution and comparing it, I cannot understand why my new_str = chars.join("") followed by return new_str would lead to an error. In my thought process I am essentially making a new variable out of the results of the iteration and returning that variable. As soon as I switch the variable name to str, the code works perfectly.

Is this possibly because the question states to return “a collapsed version of the string” and do not want a new variable returned?

My interview is Tuesday so any help before then would be greatly appreciated.