Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sockets - Evelynn #33

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
def is_vowel?(letter)
case letter
when "a", "e", "i", "o", "u"
letter = true
else
letter = false
end
return letter

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would probably not store the true or false value in letter, because we would expect letter to be a one-character string. Instead you could create a new variable (maybe vowel), or just return from inside the case statement.

end

def run_generator
puts "Please enter your first word for the portmanteau. It must be at least 2 characters long."
word_A = gets.chomp
until word_A.length >= 2
puts "I'm sorry, this word is invalid. Please enter a word that is at least 2 characters long."
word_A = gets.chomp

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Ruby, we typically name variables using all lowercases letters, so this would be word_a.

end
word_A_chop = 0
word_A_last = word_A.length - 1

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should use vertical whitespace (empty lines) to separate different pieces of your code, the same way you would use space to separate paragraphs in English text.

Just focusing on the section for the first word, I would probably put a blank line after lines 17, 17, and 33.

word_A.reverse.each_char do |c|
if !is_vowel?(c) && word_A_chop == word_A_last #Ensures words with no vowels will include the full word in portmanteau.
break
elsif is_vowel?(c)
break
end
word_A_chop += 1
end
if word_A_chop == word_A_last #Ensures words with no vowels will include the full word in portmanteau.
puts "The first half of your word is #{word_A}."
else
word_A = word_A[0...(word_A_last - word_A_chop)]
puts "The first half of your word is #{word_A}."
end
puts "Please enter your second word for the portmanteau."
word_B = gets.chomp
until word_B.length >= 2
puts "I'm sorry, this word is invalid. Please enter a word that is at least 2 characters long."
word_B = gets.chomp

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your code to get the second word is almost exactly the same as the code to get the first word. Could you DRY this up using another method?

end
word_B_chop = 0
word_B_last = word_B.length - 1
word_B.each_char do |c|
if is_vowel?(c) && word_B_chop == 0 #Ensures words beginning with vowels will include entire word in portmanteau.
break
elsif is_vowel?(c)
break
elsif !is_vowel?(c) && word_B_chop == word_B_last
word_B_chop = 0 #Reset to 0 so that all-consonant words will include entire word in portmanteau.
break
else
word_B_chop += 1
end
end
word_B = word_B[word_B_chop..word_B_last]
puts "The second half of your word is #{word_B}."
puts "\nYour portmanteau is #{word_A + word_B}."
end

run_generator