I am trying to learn Ruby with the Test-First material and I´m stuck at this point. This is my code:
def translate(word)
word=word.split("")
while (!vowel(word[0])) do
first_letter=word[0]
word.each_index do |i|
word[i]=word[i+1]
end
word[word.length-1]=first_letter
end
return word.join + "ay"
end
def vowel(letter)
if letter == ("a" || "e" || "i" || "o" || "u")
return true
end
end
The goal is to implement a function that translates a word to "pig latin" by looking if the word starts with a vowel. If this is the case, the function just appends "ay". If it´s beginning with a consonant, it puts the first letter at the end of the word until there is a vowel in the first position.
The problem I have starts when assigning the first letter, which I saved at the start of the while loop, to the last position of the word , with
word[word.length-1]=first_letter
This causes the program to loop indefinitely.
The test is based on the string "banana", it should read "ananabay" after processing. In this case, first_letter contains "b". So I tested assigning different letters to the last position of word, everything but "b" works out fine. What I don´t get is that with the while block not depending on the last letter, but on the first letter of the word, it still causes an indefinite loop assigning "b" to word[word.length-1].
I would be very thankful for some insight.
Note that
is equivalent to
as
("a" || "e" || "i" || "o" || "u")
will evaluate to first truthy value which happens to be"a"
in this case.What you need is
which can also be written as:
or
or
Due to above mentioned logic error, the
vowel
method will returnnil
whenever letter is not"a"
, and if there is a word that does not contain vowela
, then, thewhile
loop never terminates. Hence, it needs to be corrected to something like below. Also, while we are at it, lets add?
to method name to indicate that it will return a boolean value.Moving the first character of the string to end of string can also be simplified by using code like below:
Lets extract this logic into a new method:
You can also simplify the
while
loop by switching tountil
expression.Hence, your
translate
method can be simplified to something like below:There is still one issue, what happens to words that do not have any vowels, the loop will be infinite loop. We need to fix it
Complete code: