Creating a pig latin translator in ruby, having trouble with multiple words -


my code works fine 1 word, searcher proc works fine. trying work multiple words storing them in array , passing each word proc , putting them together, when test such "eat pie" returns this. can tell me i'm doing wrong?

failures:  1) translate translates 2 words  failure/error: s.should == "eatay iepay"    expected: "eatay iepay"         got: "eat pieay eat pieayay" (using ==)  # ./04_pig_latin/pig_latin_spec.rb:41:in `block (2 levels) in <top (required)>' 

heres code:

def translate(x)   array = x.split(' ')     searcher = proc.new{      if x.index(/[aeiou]/) == 0       x = x + "ay"     elsif x[0..1] == "qu"       first = x.chr       x.reverse!.chop!.reverse!       second = x.chr       x.reverse!.chop!.reverse!       x = x + first + second + "ay"    elsif x.index(/[aeiou]/) == 1      first = x.chr      x.reverse!.chop!.reverse!      x = x + first + "ay"   elsif x[1..2] == "qu"      first = x.chr      x.reverse!.chop!.reverse!      second = x.chr      x.reverse!.chop!.reverse!      third = x.chr      x.reverse!.chop!.reverse!      x = x + first + second + third +"ay"   elsif x.index(/[aeiou]/) == 2     first = x.chr.chr     x.reverse!.chop!.reverse!     second = x.chr     x.reverse!.chop!.reverse!     x = x + first + second + "ay"  elsif x.index(/[aeiou]/) == 3    first = x.chr    x.reverse!.chop!.reverse!    second = x.chr    x.reverse!.chop!.reverse!    third = x.chr    x.reverse!.chop!.reverse!    x = x + first + second + third +"ay" else   x end } if array.count == 1   searcher.call   return x else   return array.collect(&searcher).join(' ') end end 

the problem referring x in proc searcher closed on argument x passed translate, when mean process each element of array 1 @ time.

i altered structure of code easier reason - eliminating anonymous proc , used idiomatic ruby map process string - whether 1 or more words.

#!/usr/bin/env ruby  def translate_word(x)   if x.index(/[aeiou]/) == 0     x = x + "ay"   elsif x[0..1] == "qu"     first = x.chr     x.reverse!.chop!.reverse!     second = x.chr     x.reverse!.chop!.reverse!     x = x + first + second + "ay"   elsif x.index(/[aeiou]/) == 1     first = x.chr     x.reverse!.chop!.reverse!     x = x + first + "ay"   elsif x[1..2] == "qu"     first = x.chr     x.reverse!.chop!.reverse!     second = x.chr     x.reverse!.chop!.reverse!     third = x.chr     x.reverse!.chop!.reverse!     x = x + first + second + third +"ay"   elsif x.index(/[aeiou]/) == 2     first = x.chr.chr     x.reverse!.chop!.reverse!     second = x.chr     x.reverse!.chop!.reverse!     x = x + first + second + "ay"   elsif x.index(/[aeiou]/) == 3     first = x.chr     x.reverse!.chop!.reverse!     second = x.chr     x.reverse!.chop!.reverse!     third = x.chr     x.reverse!.chop!.reverse!     x = x + first + second + third +"ay"   else     x   end end  words = argv[0].split(' ').map { |word| translate_word(word) }.join(' ') puts words 

verified chmod +x pig_latin then:

./pig_latin "eat pie" ./pig_latin "eat" 

Comments