mercredi 20 mai 2020

Undefined method `shift' for "b":String (NoMethodError) Ruby

I keep getting the same error when trying to run this method that translates a string into pig latin.

def pig_latin_word(word)
  new_arr = []
  letter = word.split("")

    if letter[0] == "a" || letter[0] == "e" || letter[0] == "i" || letter[0] == "o" || 
      letter[0] == "u"
      new_arr << "yay"
    else
      letter[0].shift && new_arr << letter[0].push + "ay"
    end
  return new_arr.join

end

Test cases: 
puts pig_latin_word("eat")     # => "eatyay"
puts pig_latin_word("banana")  # => "ananabay"
puts pig_latin_word("trash")   # => "ashtray"

Aucun commentaire:

Enregistrer un commentaire