Last active
December 24, 2015 01:38
-
-
Save 404pnf/6724648 to your computer and use it in GitHub Desktop.
piglatin and rotation implemented in ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class String | |
def vowel? | |
match(/[aeiou]/i) | |
end | |
def constant? | |
!match(/[aeiou]/i) | |
end | |
def rotate(n = 1) | |
self[(n.remainder(length))..-1] + self[0..(n - 1)] | |
end | |
def rotate! | |
self.replace(rotate) | |
end | |
def rotate_r(n = 1) | |
self[-(n)..-1] + self[0..-(n.remainder(length) + 1)] | |
end | |
def rotate_r! | |
self.replace(rotate_r) | |
end | |
def rotations | |
r = [] | |
str = dup | |
each do |_| | |
r << str | |
str.rotate! | |
end | |
r | |
end | |
def piglatin | |
str = dup | |
if str[0].vowel? | |
str.rotate + 'ay' | |
else | |
str.rotate!.piglatin | |
end | |
end | |
end | |
require 'rspec' | |
RSpec.configure do |config| | |
config.treat_symbols_as_metadata_keys_with_true_values = true | |
config.expect_with :rspec do |c| | |
c.syntax = :expect # disables `should` | |
end | |
describe 'String#vowel?' do | |
it 'predicts a char is a vowel or not, ignore char case' do | |
v = %w(a e i o u A E I O U f ) | |
c = %w(m n P Q) | |
expect(v.map(&:vowel?).all?).to eq(true) # all true value in result | |
expect(c.map(&:vowel?).any?).to eq(false) # no true value in result | |
end | |
end | |
describe 'String#constant?' do | |
it 'predicts a char is a constant or not, ignore char case' do | |
v = %w(a e i o u A E I O U ) | |
c = %w(m n P Q) | |
expect(v.map(&:constant?).any?).to eq(false) # all true value in result | |
expect(c.map(&:constant?).all?).to eq(true) # no true value in result | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment