-1

So ive been trying to write the pig latin method with its different instances. However, the join method at the end simply joins the original words of the string as opposed to the modified words that have gone through the iteration:

def translate(string)
  alphabet = ("a".."z").to_a
  vowels = %w{a e i o u}
  consonant = alphabet - vowels
  words = string.split

  words.each do |word|
    if vowels.include?(word[0])
      word = word + "ay"
    elsif word[0..2] == "sch"
      word = word[3..-1] + word[0..2] + "ay"
    elsif word[0..1] == "qu"
      word = word[2..-1] + word[0..1] + "ay"
    elsif word[1..2] == "qu"
      word = word[3..-1] + word[0..2] + "ay"
    elsif consonant.include?(word[0]) && consonant.include?(word[1]) && consonant.include?(word[2])
      word = "#{word[3..-1]}#{word[0..2]}ay"
    elsif consonant.include?(word[0]) && consonant.include?(word[1])
      word = "#{word[2..-1]}#{word[0..1]}ay"
    elsif consonant.include?(word[0])
      word = "#{word[1..-1]}#{word[0]}ay"
    end
  end

  p words.join(" ") 
end


translate("apple pie")
translate("cherry")
translate("school")
translate("square")
translate("three")
translate("apple")
translate("cat")

This is what it gives me when it runs:

"apple pie"
"cherry"
"school"
"square"
"three"
"apple"
"cat"
Michael Nail
  • 101
  • 8
Saleh Rastani
  • 184
  • 1
  • 8
  • What is your question? – sawa Jul 30 '14 at 00:32
  • I wanted the method to give me the modified string, whether it was one word or more, that had undergone the iteration and change, but it gives me the original string without any changes when I join and puts the words array. how can i return the modified version of the string? That was my main question – Saleh Rastani Jul 30 '14 at 19:04

2 Answers2

1

Try using map instead of each

Like so:

def translate(string)
  alphabet = ("a".."z").to_a
  vowels = %w{a e i o u}
  consonant = alphabet - vowels
  words = string.split

  result = words.map do |word|
    if vowels.include?(word[0])
      word = word + "ay"
    elsif word[0..2] == "sch"
      word = word[3..-1] + word[0..2] + "ay"
    elsif word[0..1] == "qu"
      word = word[2..-1] + word[0..1] + "ay"
    elsif word[1..2] == "qu"
      word = word[3..-1] + word[0..2] + "ay"
    elsif consonant.include?(word[0]) && consonant.include?(word[1]) && consonant.include?(word[2])
      word = "#{word[3..-1]}#{word[0..2]}ay"
    elsif consonant.include?(word[0]) && consonant.include?(word[1])
      word = "#{word[2..-1]}#{word[0..1]}ay"
    elsif consonant.include?(word[0])
      word = "#{word[1..-1]}#{word[0]}ay"
    end
    word
  end

  p result.join(" ") 
end


translate("apple pie")
translate("cherry")
translate("school")
translate("square")
translate("three")
translate("apple")
translate("cat")

I suggest reading this answer Array#each vs. Array#map because it will shed some light on the difference between map and each. Your each block is returning the original words array, so that is why it was never changed.

Community
  • 1
  • 1
johnnyutah
  • 685
  • 1
  • 7
  • 23
  • Thanks, it was both map and the fact that you had the iteration put as result, and finally joined whatever was in result. Thank you – Saleh Rastani Jul 30 '14 at 18:55
0

Using regular expressions and a case statement, you can compact it quite a bit:

def pig_latin(word)
  case word
  when /^[aeiou]/ #starts with a vowel
    word + "ay"
  when /^([s?qu|[^aeiou]{1,3})/ #starts with qu, squ or 1-3 consonants
    rep = $1
    word.sub(rep, "") + "#{rep}ay"
  end
end

def translate(string)
  string.split.map { |word| pig_latin(word) }.join " "
end

resulting in the following tests being all true:

puts translate("apple pie") == "appleay iepay"
puts translate("cherry") == "errychay"
puts translate("school") == "oolschay"
puts translate("square") == "aresquay"
puts translate("three") == "eethray"
puts translate("apple") == "appleay"
puts translate("cat") == "atcay"

Note that I simplified the second regular expression with the assumption that the only consonant that can precede "qu" at the beginning of a word is "s" (which is the case according to /usr/share/dict/words).

Mark Thomas
  • 37,131
  • 11
  • 74
  • 101
  • Thank you for the response. Incredible coding approach. This is going to need some learning, on my part to thoroughly grasp. Thank you – Saleh Rastani Jul 30 '14 at 18:57