Ruby doesn't recognize `downcase` or `split` as defined methods? [duplicate] - ruby

This question already has answers here:
Why word 'translate' is messing irb?
(2 answers)
Closed 6 years ago.
The issue is with the following code:
#write your code here
def translate phrase
phrase = phrase.downcase.split(/ /)
phrase.collect! do |word|
word = word.split(//)
switched = false
while switched == false
word.each_index do |letter|
if word[letter] == ("a" || "e" || "i" || "o" || "u")
switched = true
word = (word[letter..-1] + word[0..(letter-1)]).join + "ay"
end
end
end
end
return phrase.join(" ")
end
puts translate("chocolate cream")
#Should return "ocolatechay eamcray"
When I run this, Ruby just returns a blank line. So, to troubleshoot the issue, I loaded the definition into a repl. The repl returned the following error:
NoMethodError: undefined method `downcase' for #<RubyVM::InstructionSequence:0x000000016e8f88>
from /home/adc/odin-project/web-development-101/21-ruby-tdd/ruby_tdd_project/learn_ruby/04_pig_latin/pig_latin.rb:3:in `translate'
from /home/adc/.rvm/rubies/ruby-2.3.0/bin/irb:11:in `<main>'
If I remove downcase from my code, I get the same error message, only this time with split.
What's the problem here? (I'm pretty confident that downcase and split are not the problem.)

If returns a blank string because you don't return anything inside the collect! block. Return word and it will work :
def translate phrase
phrase = phrase.downcase.split(/ /)
phrase.collect! do |word|
word = word.split(//)
switched = false
while switched == false
word.each_index do |letter|
if word[letter] == ("a" || "e" || "i" || "o" || "u")
switched = true
word = (word[letter..-1] + word[0..(letter-1)]).join + "ay"
end
end
end
word
end
return phrase.join(" ")
end
puts translate("chocolate cream")
#=> atechocolay amcreay
It doesn't look like it's returning exactly what you expected, but it's still better than a blank string.
As for your weird error message in the console, it seems to be specific to the REPL (possibly because of the method name translate).

Update 1 (how I would write this code)
def translate phrase
phrase = phrase.downcase.split(/ /)
phrase.collect! do |word|
word = word.split(//)
word.each_index do |letter|
if ["a", "e", "i", "o", "u"].include?(word[letter])
if letter == 0
word = word[letter..-1].join + "ay"
else
word = (word[letter..-1] + word[0..(letter-1)]).join + "ay"
end
break
end
end
word
end
return phrase.join(" ")
end
puts translate("chocolate cream")
Update 2 (if I make some minor changes to your code to make it useful)
def translate phrase
phrase = phrase.downcase.split(/ /)
phrase.collect! do |word|
word = word.split(//)
switched = false
while switched == false
word.each_index do |letter|
if ["a", "e", "i", "o", "u"].include?(word[letter])
switched = true
if letter == 0
word = word[letter..-1].join + "ay"
else
word = (word[letter..-1] + word[0..(letter-1)]).join + "ay"
end
break
end
end
end
word
end
return phrase.join(" ")
end
puts translate("chocolate cream")
Explanation
"o"==("a" || "e" || "i" || "o" || "u")
and
"o"== "a" || "o" == "e" || "o" == "i" || "o" == "o" || "o" == "u"
statements are not the same. First one is false
(because ("a" || "e" || "i" || "o" || "u")=="a" and "o"!="a"), since the second one is true.
You need inner if statement
if letter == 0
word = word[letter..-1].join + "ay"
else
word = (word[letter..-1] + word[0..(letter-1)]).join + "ay"
end
because when word starts with any mentioned vowels then
word[letter..-1] + word[0..(letter-1)]
statement will return the whole word twice, cause letter will equal to 0.
And lastly you need to return word object after the while loop.

Related

if statement doesn't get executed in Ruby

I'm new to Ruby programming language and i am asked to make a small program that does the following:
Rule 1: If a word begins with a vowel sound, add an "ay" sound to the end of the word.
Rule 2: If a word begins with a consonant sound, move it to the end of the word, and then add an "ay" sound to the end of the word.
but in my if else statement it doesn't go into the if even if its true it stays at the else statement
i have tried taking the string and converting it into an array and work on the array and tried working on the string as is
def translate (str)
i = 0
while i < str.length
if (str[i] == "a" or str[i] == "e" or str[i] == "o" or str[i] == "u" or str[i] == "i")
str = str + "ay"
return str
else
temp = str[0...1]
str = str[1...str.length]
str = str + temp
end
i = i + 1
end
end
s = translate("banana")
puts s
the program doesn't enter the if statement at all and keeps getting into the else statement until the word returns the same with out any changes
Aside from my suggestion to use || instead of or, your method doesn't need a #while iterator since you're checking only for the first letter. The if/else statement should be executed only once.
You can also replace all the checks with a single #include? method like this:
def translate (str)
if %w[a e i o u].include?(str[0])
str + "ay"
else
str[1..-1] + str[0] + "ay"
end
end
Notice that I've also removed the return statement since the last executed line will be returned, so either line 3 or line 5 in the method above.
You can also add a ternary operator to make it in one line:
%w(a e i o u).include?(str[0]) ? str + "ay" : str[1..-1] + str[0] + "ay"
Rule 1: If a word begins with a vowel sound, add an "ay" sound to the
end of the word.
translate("ana")
# ~> "anaay"
Rule 2: If a word begins with a consonant sound, move it to the end of
the word, and then add an "ay" sound to the end of the word.
translate("banana")
# ~> "ananabay"
If I understand the problem correctly, you do not need to loop at all here. You just need to check the first letter, and not all of the letters.
def translate (str)
if str[0] == 'a' or str[0] == 'e' or str[0] == 'o' or str[0] == 'u' or str[0] == 'i'
str + 'ay'
else
temp = str[0...1]
str = str[1...str.length]
str = str + temp
str + 'ay'
end
end
By the way, I was able to figure this out with the debugger. Did you try that at all? Also, with your original code it turns out that for some inputs (like 'baaan'), your else statement does execute.
I don't see a problem with or or || in this case.
The problem I see is that if the start letter is a consonant, you changing str rotating it's letters at each iteration (see the commented part of the code), so the starting letter is never a vowel.
Then you are missing a returning value at the end so it returns nil and puts nothing.
def translate (str)
i = 0
while i < str.length
p str[i] # it's never a vowel
if (str[i] == "a" or str[i] == "e" or str[i] == "o" or str[i] == "u" or str[i] == "i")
str = str + "ay"
return str
else # here you are rotating the word
temp = str[0...1]
str = str[1...str.length]
str = str + temp
p str
end
i = i + 1
end
# missing a return value
end
s = translate("banana")
p s
So it prints out:
# "b"
# "ananab"
# "n"
# "nanaba"
# "n"
# "anaban"
# "b"
# "nabana"
# "n"
# "abanan"
# "n"
# "banana"
# nil
The code works correctly in case the first letter is a vowel, so it enters the if true:
s = translate("ananas")
p s
#=> "ananasay"
By the way, as already posted by others, you don't need any while loop. Just checking the first letter with an if statement is enough.

How to implement a for loop in a method in Ruby

I have been tasked to create a pig_latin method.
Pig Latin is a made-up children's language that's intended to be
confusing. It obeys a few simple rules (below) but when it's spoken
quickly it's really difficult for non-children (and non-native
speakers) to understand.
Rule 1: If a word begins with a vowel sound, add an "ay" sound to
the end of the word.
Rule 2: If a word begins with a consonant sound, move it to the end
of the word, and then add an "ay" sound to the end of the word.
(There are a few more rules for edge cases, and there are regional
variants too, but that should be enough to understand the tests.)
All my tests are passing save one, to translate many words.
This is my error:
#translate
translates a word beginning with a vowel
translates a word beginning with a consonant
translates a word beginning with two consonants
translates two words
translates a word beginning with three consonants
counts 'sch' as a single phoneme
counts 'qu' as a single phoneme
counts 'qu' as a consonant even when it's preceded by a consonant
translates many words (FAILED - 1)
Failures:
1) #translate translates many words
Failure/Error: expect(s).to eq("ethay ickquay ownbray oxfay")
expected: "ethay ickquay ownbray oxfay"
got: "ethay"
(compared using ==)
# ./spec/04_pig_latin_spec.rb:70:in `block (2 levels) in <top (required)>'
Finished in 0.00236 seconds (files took 0.10848 seconds to load)
9 examples, 1 failure
Failed examples:
rspec ./spec/04_pig_latin_spec.rb:68 # #translate translates many words
And this is my method:
def translate(str)
def add_ay(str)
return str + 'ay'
end
def word_begins_with_vowel(str)
if (!(str.match(' '))) && $vowels[str[0]]
return add_ay(str)
end
end
def begins_with_consonant(str)
if ((!$vowels[str[0]]) && (!$vowels[str[1]]) && (!$vowels[str[2]]))
first_three = str.split('').slice(0, 3).join('');
str = str.slice(3, str.length - 1)
return str + first_three + 'ay'
end
if ((!$vowels[str[0]]) && (!$vowels[str[1]]))
first_two = str.split('').slice(0, 2).join('');
str = str.slice(2, str.length - 1)
return str + first_two + 'ay'
end
if ((!$vowels[str[0]]))
first_char = str.split('').slice(0);
str = str.slice(1, str.length - 1)
return str + first_char +'ay'
end
end
def translates_two_words(str)
if (str.match(' '))
str = str.split(' ');
first_char = str[1].split('').slice(0);
str[1] = str[1].slice!(1, str[1].length - 1);
return str[0] + 'ay' + ' ' + str[1] + first_char + 'ay'
end
end
def translates_many_words(str)
str = str.split(' ');
if str.length > 2
str.each do |item|
return begins_with_consonant(item) || word_begins_with_vowel(item)
end
end
end
$vowels = {
'a' => add_ay(str),
'e' => add_ay(str),
'i' => add_ay(str),
'o' => add_ay(str),
'y' => add_ay(str)
}
return translates_many_words(str) || word_begins_with_vowel(str) || begins_with_consonant(str) || translates_two_words(str)
end
I would figure this would take care of many words:
def translates_many_words(str)
str = str.split(' ');
if str.length > 2
str.each do |item|
return begins_with_consonant(item) || word_begins_with_vowel(item)
end
end
end
but it's not.
As #theTinMan says, return - will decline next iteration and just return the first value in the first iteration, from my comment, I think, this should work for you(with minimum editing of your code):
def translates_many_words(str)
str = str.split(' ');
if str.length > 2
str.map do |item|
begins_with_consonant(item) || word_begins_with_vowel(item)
end.join(' ')
end
end
UPD
Also, I'll recommend you to refactor your code to make it more readable, it could help you in the future.
My variant of this method is:
def translates_many_words(str)
str = str.split
# line under - is a shortcut from `return nil if str.size <= 2`
# `#size` is more relative to this context if you will count elements of array
return unless str.size > 2
# Now, when we excluded possibility of work with array that have less then 2 elements,
# we can continue with our iteration
str.map do |item|
begins_with_consonant(item) || word_begins_with_vowel(item)
end.join(' ')
end

Ruby map function for "pig latin"

I am attempting to write a ruby function that takes in a string of words and turns it into Pig Latin. I am breaking the string into an array, and attempting to iterate over each element. When "eat pie" is put in, the result is "eat ie", but I am unsure why.
string = "eat pie"
array = string.split(" ")
array.map do |word|
if word[0].chr == "a" || word[0].chr == "e" || word[0].chr == "i" || word[0].chr == "o" || word[0].chr == "u"
word = word + "ay"
elsif word[1].chr == "a" || word[1].chr == "i" || word[1].chr == "o" || word[1].chr == "u"
temp = word[0]
word[0] = ""
word = word + temp
word = word + "ay"
elsif word[2].chr == "a" || word[2].chr == "i" || word[2].chr == "o" || word[2].chr == "u"
temp = word[0] + word[1]
word[0] = ""
word[0] = ""
word = word + temp
word = word + "ay"
else ## three consonants
temp = word[0] + word[1] + word[2]
word[0] = ""
word[0] = ""
word[0] = ""
word = word + temp
word = word + "ay"
end ## end of if statement
end ## end of iteration
puts array.join(" ")
Agree with the other answers supplied, here's a slightly less verbose version of your code in case it helps you.
input = 'pig latin is awesome'
arr = input.split(' ').map do |wrd|
if %w(a e i o u).include? wrd[0]
wrd + 'ay'
elsif %w(a i o u).include? wrd[1]
wrd[1..-1] + wrd[0] + 'ay'
elsif %w(a i o u).include? wrd[2]
wrd[2..-1] + wrd[0] + wrd[1] + 'ay'
else
wrd[3..-1] + wrd[0] + wrd[1] + wrd[2] + 'ay'
end
end.join(' ')
puts arr
The output you are seeing has 2 different causes:
1) In Ruby, Array.map returns a new array. It does not modify the array its iterating over. This is causing some of your modifications to the array to be lost.
From the Ruby docs:
Invokes the given block once for each element of self.
Creates a new array containing the values returned by the block.
You should either assign the result to a new variable, or use Array.map! instead, which will modify the contents of the array.
array = string.split(" ")
mapped = array.map do |word|
# ...
end
mapped.join(" ")
2) Although some of your modifications are being lost due to using map, you are making modifications to some strings in your array, which is why you are seeing eat ie, and not eat pie (missing a p).
To illustrate this problem, look a the follow code:
word = "pie"
word[0] = ""
puts word #=> "ie"
In Ruby, when you access the first character in a string (by using [0]), and assign a value to it, Ruby mutates that string, and does not return a new copy.
You should create a new string, instead of changing characters directly:
array.map do |word|
new_word = word.slice(1, word.length)
end
array.map does is not intended to mutate the array, so what you want to do is either newarray = array.map { ... } or array.map! do ... end.
Inside the loop, each word is a string object, that you are mutating by calling word[0] = "". By calling word = word + "ay" you are discarding the reference of the original string, but not overwriting it. It happens though that word = word + "ay" is the last statement executed in the block so it counts as return value for the block.
I bet you are still confused because overall is not that simple so you might probably want to read something about mutable and immutable objects and functional and imperative programming.

How can I check for first letter(s) in string in Ruby?

I'm writing test file, but I can't get it pass second test, here:
def translate(word)
if word.start_with?('a','e','i','o','u')
word << "ay"
else
word << "bay"
end
end
Is start_with? the right method to do the job?
describe "#translate" do
it "translates a word beginning with a vowel" do
s = translate("apple")
s.should == "appleay"
end
it "translates a word beginning with a consonant" do
s = translate("banana")
s.should == "ananabay"
end
it "translates a word beginning with two consonants" do
s = translate("cherry")
s.should == "errychay"
end
end
EDIT:
My solution is not complete.
My code pass first test only because I was able to push "ay" to the end of word. What I'm missing to pass the second test is to remove the first letter if its consonant, which is "b" in "banana".
You can do this also:
word << %w(a e i o u).include?(word[0]) ? 'ay' : 'bay'
Using a Regex might be overkill in your case, but could be handy if you want to match more complex strings.
word << word[0].match(/a|e|i|o|u/).nil? ? 'bay' : 'ay'
Your code means:
if word start with ('a','e','i','o','u') add "ay" at the end
else add "bay" at the end.
Second test will be "bananabay" and not "ananabay" (with b as first letter)
def translate(word)
prefix = word[0, %w(a e i o u).map{|vowel| "#{word}aeiou".index(vowel)}.min]
"#{word[prefix.length..-1]}#{prefix}ay"
end
puts translate("apple") #=> "appleay"
puts translate("banana") #=> "ananabay"
puts translate("cherry") #=> "errychay"
Looks like you are removing the first character if the word starts with a consonant too, so:
if word.start_with?('a','e','i','o','u')
word[0] = ''
word << 'ay'
else
consonant = word[0]
word << "#{consonant}ay"
end
The below piece of code passes all the tests...
def translate(word)
if word.start_with?('a','e','i','o','u')
word<<'ay'
else
pos=nil
['a','e','i','o','u'].each do |vowel|
pos = word.index(vowel)
break unless pos.nil?
end
unless pos.nil?
pre = word.partition(word[pos,1]).first
word.slice!(pre)
word<<pre+'ay'
else
#code to be executed when no vowels are there in the word
#eg words fry,dry
end
end
end
Figured I'd share my first contribution!
Good luck!
def method(word)
word[0].eql?("A" || "E" || "I" || "O" || "U")
end

Pig-Latin method translation

Trying to write Method in ruby that will translate a string in pig-latin , the rule :
Rule 1: If a word begins with a vowel sound, add an "ay" sound to the end of the word.
Rule 2: If a word begins with a consonant sound, move it to the end of the word, and then add an "ay" sound to the end of the word and also when the word begins with 2 consonants , move both to the end of the word and add an "ay"
As a newbie , my prob is the second rule , when the word begin with only one consonant it work , but for more than one , I have trouble to make it work ,Can somebody look at the code and let me know how i can code that differently and probably what is my mistake , probably the code need refactoring. Thanks , so far i come up with this code :
def translate (str)
str1="aeiou"
str2=(/\A[aeiou]/)
vowel = str1.scan(/\w/)
alpha =('a'..'z').to_a
con = (alpha - vowel).join
word = str.scan(/\w/)
if #first rule
str =~ str2
str + "ay"
elsif # second rule
str != str2
s = str.slice!(/^./)
str + s + "ay"
elsif
word[0.1]=~(/\A[con]/)
s = str.slice!(/^../)
str + s + "ay"
else
word[0..2]=~(/\A[con]/)
s = str.slice!(/^.../)
str + s + "ay"
end
end
translate("apple") should == "appleay"
translate("cherry") should == "errychay"
translate("three") should == "eethray"
No need for all those fancy regexes. Keep it simple.
def translate str
alpha = ('a'..'z').to_a
vowels = %w[a e i o u]
consonants = alpha - vowels
if vowels.include?(str[0])
str + 'ay'
elsif consonants.include?(str[0]) && consonants.include?(str[1])
str[2..-1] + str[0..1] + 'ay'
elsif consonants.include?(str[0])
str[1..-1] + str[0] + 'ay'
else
str # return unchanged
end
end
translate 'apple' # => "appleay"
translate 'cherry' # => "errychay"
translate 'dog' # => "ogday"
This will handle multiple words, punctuation, and words like 'queer' = 'eerquay' and 'school' = 'oolschay'.
def translate (sent)
vowels = %w{a e i o u}
sent.gsub(/(\A|\s)\w+/) do |str|
str.strip!
while not vowels.include? str[0] or (str[0] == 'u' and str[-1] == 'q')
str += str[0]
str = str[1..-1]
end
str = ' ' + str + 'ay'
end.strip
end
okay this is an epic pig latin translator that I'm sure could use a bit of refactoring, but passes the tests
def translate(sent)
sent = sent.downcase
vowels = ['a', 'e', 'i', 'o', 'u']
words = sent.split(' ')
result = []
words.each_with_index do |word, i|
translation = ''
qu = false
if vowels.include? word[0]
translation = word + 'ay'
result.push(translation)
else
word = word.split('')
count = 0
word.each_with_index do |char, index|
if vowels.include? char
# handle words that start with 'qu'
if char == 'u' and translation[-1] == 'q'
qu = true
translation = words[i][count + 1..words[i].length] + translation + 'uay'
result.push(translation)
next
end
break
else
# handle words with 'qu' in middle
if char == 'q' and word[i+1] == 'u'
qu = true
translation = words[i][count + 2..words[i].length] + 'quay'
result.push(translation)
next
else
translation += char
end
count += 1
end
end
# translation of consonant words without qu
if not qu
translation = words[i][count..words[i].length] + translation + 'ay'
result.push(translation)
end
end
end
result.join(' ')
end
So this will give the following:
puts translate('apple') # "appleay"
puts translate("quiet") # "ietquay"
puts translate("square") # "aresquay"
puts translate("the quick brown fox") # "ethay ickquay ownbray oxfay"
def translate(sentence)
sentence.split(" ").map do |word|
word = word.gsub("qu", " ")
word.gsub!(/^([^aeiou]*)(.*)/,'\2\1ay')
word = word.gsub(" ", "qu")
end
end
That was fun! I don't like the hack for qu, but I couldn't find a nice way to do that.
So for this pig latin clearly I skipped and\an\in and singular things like a\I etc. I know that wasn't the main question but you can just leave out that logic if it's not for your use case. Also this goes for triple consonants if you want to keep it with one or two consonants then change the expression from {1,3} to {1,2}
All pig latin is similar so just alter for your use case. This is a good opportunity to use MatchData objects. Also vowel?(first_letter=word[0].downcase) is a style choice made to be more literate so I don't have to remember that word[0] is the first letter.
My answer is originally based off of Sergio Tulentsev's answer in this thread.
def to_pig_latin(sentence)
sentence.gsub('.','').split(' ').collect do |word|
translate word
end.compact.join(' ')
end
def translate(word)
if word.length > 1
if word == 'and' || word == 'an' || word == 'in'
word
elsif capture = consonant_expression.match(word)
capture.post_match.to_s + capture.to_s + 'ay'
elsif vowel?(first_letter=word[0].downcase)
word + 'ay'
elsif vowel?(last_letter=word[-1].downcase)
move_last_letter(word) + 'ay'
end
else
word
end
end
# Move last letter to beginning of word
def move_last_letter(word)
word[-1] + word[0..-2]
end
private
def consonant_expression
# at the beginning of a String
# capture anything not a vowel (consonants)
# capture 1, 2 or 3 occurences
# ignore case and whitespace
/^ [^aeiou] {1,3}/ix
end
def vowel?(letter)
vowels.include?(letter)
end
def vowels
%w[a e i o u]
end
Also just for the heck of it I'll include my dump from a pry session so you all can see how to use MatchData. MINSWAN. It's stuff like this that makes ruby great.
pry > def consonant_expression
pry * /^ [^aeiou] {1,3}/ix
pry * end
=> :consonant_expression
pry > consonant_expression.match('Stream')
=> #<MatchData "Str">
pry > capture = _
=> #<MatchData "Str">
pry > ls capture
MatchData#methods:
== begin end hash length offset pre_match regexp string to_s
[] captures eql? inspect names post_match pretty_print size to_a values_at
pry >
pry > capture.post_match
=> "eam"
pry > capture
=> #<MatchData "Str">
pry > capture.to_s
=> "Str"
pry > capture.post_match.to_s
=> "eam"
pry > capture.post_match.to_s + capture.to_s + 'ay'
=> "eamStray"
pry >
If I understood your question correctly, you can just directly check if a character is a vowel or consonant and then use array ranges to get the part of the string you want.
vowels = ['a', 'e', 'i', 'o', 'u']
consonants = ('a'..'z').to_a - vowels
return str + "ay" if vowels.include?(str[0])
if consonants.include?(str[0])
return str[2..-1] + str[0..1] + "ay" if consonants.include?(str[1])
return str[1..-1] + str[0] + "ay"
end
str
Here's a solution that handles the "qu" phoneme as well as other irregular characters. Had a little trouble putting the individual words back into a string with the proper spacing. Would appreciate any feedback!
def translate(str)
vowels = ["a", "e", "i", "o", "u"]
new_word = ""
str.split.each do |word|
vowel_idx = 0
if vowels.include? word[0]
vowel_idx = 0
elsif word.include? "qu"
until word[vowel_idx-2]+word[vowel_idx-1] == "qu"
vowel_idx += 1
end
else
until vowels.include? word[vowel_idx]
vowel_idx += 1
end
end
idx_right = vowel_idx
while idx_right < word.length
new_word += word[idx_right]
idx_right += 1
end
idx_left = 0
while idx_left < vowel_idx
new_word += word[idx_left]
idx_left += 1
end
new_word += "ay "
end
new_word.chomp(" ")
end
I done gone did one too
def translate(string)
vowels = %w{a e i o u}
phrase = string.split(" ")
phrase.map! do |word|
letters = word.split("")
find_vowel = letters.index do |letter|
vowels.include?(letter)
end
#turn "square" into "aresquay"
if letters[find_vowel] == "u"
find_vowel += 1
end
letters.rotate!(find_vowel)
letters.push("ay")
letters.join
end
return phrase.join(" ")
end
def piglatinize(word)
vowels = %w{a e i o u}
word.each_char do |chr|
index = word.index(chr)
if index != 0 && vowels.include?(chr.downcase)
consonants = word.slice!(0..index-1)
return word + consonants + "ay"
elsif index == 0 && vowels.include?(chr.downcase)
return word + "ay"
end
end
end
def to_pig_latin(sentence)
sentence.split(" ").collect { |word| piglatinize(word) }.join(" ")
end
This seems to handle all that I've thrown at it including the 'qu' phoneme rule...
def translate str
letters = ('a'..'z').to_a
vowels = %w[a e i o u]
consonants = letters - vowels
str2 = str.gsub(/\w+/) do|word|
if vowels.include?(word.downcase[0])
word+'ay'
elsif (word.include? 'qu')
idx = word.index(/[aeio]/)
word = word[idx, word.length-idx] + word[0,idx]+ 'ay'
else
idx = word.index(/[aeiou]/)
word = word[idx, word.length-idx] + word[0,idx]+'ay'
end
end
end
I'm grabbing the words with the 'qu' phoneme and then checking all the other vowels [excluding u].
Then I split the word by the index of the first vowel (or vowel without 'u' for the 'qu' cases) and dropping the word part before that index to the back of the word. And adding 'ay' ftw.
Many of the examples here are fairly long. Here's some relatively short code I came up with. It handles all cases including the "qu" problem! Feedback always appreciated (I'm pretty new to coding).
$vowels = "aeiou"
#First, I define a method that handle's a word starting with a consonant
def consonant(s)
n = 0
while n < s.length
if $vowels.include?(s[n]) && s[n-1..n] != "qu"
return "#{s[n..-1]}#{s[0..n-1]}ay"
else
n += 1
end
end
end
#Then, I write the main translate method that decides how to approach the word.
def translate(s)
s.split.map{ |s| $vowels.include?(s[0]) ? "#{s}ay" : consonant(s) }.join(" ")
end

Resources