I'm trying to write code in Ruby that removes all the vowels from a string:
def remvowel(string)
i = 0
dv_string = []
while i < string.length
if (string[i] != "a" || string[i] != "e" || string[i] != "i" || string[i] != "o" || string[i] != "u")
dv_string.push(i)
i += 1
end
i += 1
end
return dv_string.join
end
But it's not coming out right. When I run remvowel("duck")
, it returns "02"
, as in the index positions of "dc"
. I'm missing something, but I don't know what.
while
andfor
loops in ruby code, as there almost always is a better way of doing same thing (most commonly you'll seeeach
method used) – BroiSatse Jan 22 '16 at 22:53+= 1
is often (but not always) a code smell in Ruby. – sawa Jan 22 '16 at 23:46while
loop above can be easily substituted withtimes
method:string.length.times do |i|
without need of manuali
incrementation. – BroiSatse Jan 23 '16 at 0:11