Sha256: 8874f63c7e0daa6cf053db878a7287409a14a1ceaff0bf62b6773229d982956c

Contents?: true

Size: 671 Bytes

Versions: 129

Compression:

Stored size: 671 Bytes

Contents

class PigLatin
  def self.translate(phrase)
    phrase.split(' ').map do |word|
      PigLatin.new(word).translate
    end.join(' ')
  end

  def initialize(word)
    @word = word.downcase.gsub(/[^a-z]/, '')
  end

  def translate
    return (word + 'ay') if it_starts_with_vowel_sound?
    start, remainder = parse_initial_consonant_sound_and_remainder
    remainder + start + 'ay'
  end

  private

  attr_reader :word

  def it_starts_with_vowel_sound?
    word.match /\A([aeiou]|y[^aeiou]|xr)/
  end

  def parse_initial_consonant_sound_and_remainder
    word.scan(/\A([^aeiou]?qu|[^aeiou]+(?=y)|[^aeiou]+)(.*)/).first
  end
end

module BookKeeping
  VERSION = 2
end

Version data entries

129 entries across 129 versions & 1 rubygems

Version Path
trackler-2.2.1.98 tracks/ruby/exercises/pig-latin/.meta/solutions/pig_latin.rb
trackler-2.2.1.97 tracks/ruby/exercises/pig-latin/.meta/solutions/pig_latin.rb
trackler-2.2.1.96 tracks/ruby/exercises/pig-latin/.meta/solutions/pig_latin.rb
trackler-2.2.1.95 tracks/ruby/exercises/pig-latin/.meta/solutions/pig_latin.rb
trackler-2.2.1.94 tracks/ruby/exercises/pig-latin/.meta/solutions/pig_latin.rb
trackler-2.2.1.93 tracks/ruby/exercises/pig-latin/.meta/solutions/pig_latin.rb
trackler-2.2.1.92 tracks/ruby/exercises/pig-latin/.meta/solutions/pig_latin.rb
trackler-2.2.1.91 tracks/ruby/exercises/pig-latin/.meta/solutions/pig_latin.rb
trackler-2.2.1.90 tracks/ruby/exercises/pig-latin/.meta/solutions/pig_latin.rb
trackler-2.2.1.89 tracks/ruby/exercises/pig-latin/.meta/solutions/pig_latin.rb
trackler-2.2.1.88 tracks/ruby/exercises/pig-latin/.meta/solutions/pig_latin.rb
trackler-2.2.1.87 tracks/ruby/exercises/pig-latin/.meta/solutions/pig_latin.rb
trackler-2.2.1.86 tracks/ruby/exercises/pig-latin/.meta/solutions/pig_latin.rb
trackler-2.2.1.85 tracks/ruby/exercises/pig-latin/.meta/solutions/pig_latin.rb
trackler-2.2.1.84 tracks/ruby/exercises/pig-latin/.meta/solutions/pig_latin.rb
trackler-2.2.1.83 tracks/ruby/exercises/pig-latin/.meta/solutions/pig_latin.rb
trackler-2.2.1.82 tracks/ruby/exercises/pig-latin/.meta/solutions/pig_latin.rb
trackler-2.2.1.81 tracks/ruby/exercises/pig-latin/.meta/solutions/pig_latin.rb
trackler-2.2.1.80 tracks/ruby/exercises/pig-latin/.meta/solutions/pig_latin.rb
trackler-2.2.1.79 tracks/ruby/exercises/pig-latin/.meta/solutions/pig_latin.rb