.start_with? method not recognizing substring 'a'

I want to translate a string into latin pig. The rules are as follows:

  • Valid words contain two or more letters.
  • If the word begins with a consonant (letter, distinct from 'a'

    , 'e'

    , 'i'

    , 'o'

    or 'u'

    ), then the first letter is shifted to the end of the word.
  • Then add 'ay'

    .

I managed to find a way:

 def translate(word)
      if word.size <= 2
         word
      elsif
          word.size > 2
         !word.start_with?('a', 'e', 'i', 'o', 'u')
          x = word.reverse.chop.reverse
          x.insert(-1, word[0])
          x << "ay"
      else
          word << "ay"
      end
    end

      

However, my test fails for certain lines,

Test Passed: Value == "c"
Test Passed: Value == "pklqfay"
Test Passed: Value == "yykay"
Test Passed: Value == "fqhzcbjay"
Test Passed: Value == "ndnrzzrhgtay"
Test Passed: Value == "dsvjray"
Test Passed: Value == "qnrgdfay"
Test Passed: Value == "npfay"
Test Passed: Value == "ldyuqpewypay"
Test Passed: Value == "arqokudmuxay"
Test Passed: Value == "spvhxay"
Test Passed: Value == "firvmanxay"
Expected: 'aeijezpbay' - Expected: "aeijezpbay", instead got: "eijezpbaay"
Expected: 'etafhuay' - Expected: "etafhuay", instead got: "tafhueay"

      

These tests pass:

Test.assert_equals(translate("billy"),"illybay","Expected: 'illybay'")
Test.assert_equals(translate("emily"),"emilyay","Expected: 'emilyay'")

      

I'm not sure why.

+3


source to share


2 answers


If the length is word

greater than or equal to 2 of the returned word, if not, then follow the step start_with

, but when will the statement be executed else

?

Try changing the length validity to only less than 2, then check if the word "start_with" is a vowel and only return the plus word ay

, and if not the first step of the character rotation add a ay

part like:



def translate(word)
  if word.size < 2
    word
  elsif word.start_with?('a', 'e', 'i', 'o', 'u')
    word << "ay"
  else
    x = word.reverse.chop.reverse
    x.insert(-1, word[0])
    x << "ay"
  end
end

      

+2


source


Your code crashes because it doesn't really evaluate if a word starts with a constant, you check it but you don't do anything, its just an isolated string:

!word.start_with?('a', 'e', 'i', 'o', 'u')

      

Try including this line inside a if

, for example:



def translate(word)
  if word.size <= 2
    word
  else
    if !word.start_with?('a', 'e', 'i', 'o', 'u')
      x = word.reverse.chop.reverse
      x.insert(-1, word[0])
      x << "ay"
    else
      word << "ay"
    end
  end
end

      

Also note that I removed if word.size > 2

this is not necessary as you are already checking for word.size <= 2

, so nothing else but that > 2

.

+2


source







All Articles