Array Enumeration Not Working Properly?

kkSlider

I'm creating a method (Pig Latin) and am having trouble with this example test case: translate("eat pie") should return "eatay iepay" but my code doesn't seem to be making the proper changes.

def translate(phrase)
  phrase = phrase.split.each do |word|
    if ['a', 'e', 'i', 'o', 'u'].include?(word[0])
      word = word + 'ay'
    else
      ['a', 'e', 'i', 'o', 'u'].include?(word[1])? 
      word = word[1..-1] + word[0] + 'ay' : word = word[2..-1] + word[0..1] + 'ay'
    end
  end
  phrase.join(' ')
end

The problem I'm having is that the array enumeration doesn't manipulate each word at all. I'm at a loss for why that is. Thanks in advance.

Borodin

The each method simply executes the block for each element of a list. Modifying the control variable word won't affect the original element of the list.

I suggest you use the map method, which transforms one list into another. It passes each element of the original list to the block, and replaces it with the value returned by the block.

Also, you should only use the conditional operator a ? b : d to return a value - you shouldn't modify any variables. So you could write

word = ['a', 'e', 'i', 'o', 'u'].include?(word[1]) ?
    word[1..-1] + word[0] + 'ay' :
    word[2..-1] + word[0..1] + 'ay'

but here it is best to just use an additional elsif clause.

This code does what you want.

def translate(phrase)

  phrase = phrase.split.map do |word|
    if ['a', 'e', 'i', 'o', 'u'].include?(word[0])
      word + 'ay'
    elsif ['a', 'e', 'i', 'o', 'u'].include?(word[1])
      word[1..-1] + word[0] + 'ay'
    else
      word[2..-1] + word[0..1] + 'ay'
    end
  end

  phrase.join(' ')
end

p translate "eat pie"

output

"eatay iepay"

Update

This is much better written using regular expressions. You may be interested in this variation

def translate(phrase)
  phrase.split.map { |word| word.sub /^([^aeiou]*)(.+)/, '\2\1ay' }.join ' '
end

p translate 'pig latin'

output

"igpay atinlay"

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Sorting an array of enumeration

分類Dev

Converting pointer to string and send as char array not working properly

分類Dev

Software Updater is not working properly

分類Dev

Bluetooth not working properly?

分類Dev

Go pprof not working properly

分類Dev

Jquery in XPages not working properly

分類Dev

Elasticsearch asciifolding not working properly

分類Dev

AngularJS checkbox not working properly

分類Dev

Python Xlsxwriter not working properly

分類Dev

isPaused not working properly in SKNode()

分類Dev

scrollTop() with slideDown() Not Working Properly

分類Dev

Flexbox not working properly with Firefox

分類Dev

Animation not working properly in flutter?

分類Dev

Select AS is not working properly with jdbc

分類Dev

Stack count not working properly

分類Dev

JQuery mask not working properly

分類Dev

jQuery show() is not working properly

分類Dev

Fitviewport not working properly

分類Dev

Prototype javascript not working properly

分類Dev

Gson().toJson(this) not working properly

分類Dev

MediaPlayer not working properly (Android)?

分類Dev

Join not working properly

分類Dev

$lookup in aggregation working properly in mongo shell command, but while tried by using mongoose node, then getting empty array as response

分類Dev

gnome-bluetooth not working properly

分類Dev

Wine games resolution not working properly

分類Dev

Scanner and JOptionPane not working properly together

分類Dev

PostgreSQL LEFT JOIN not working properly

分類Dev

PostgreSQL LEFT JOIN not working properly

分類Dev

Chrome extension setTimeout not working properly