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 - Sarah #43

Open
wants to merge 2 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
73 changes: 73 additions & 0 deletions generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
def is_vowel?(letter)
vowels = ["a", "e", "i", "o", "u"]
if vowels.include?(letter.downcase)
return true
end
return false
end


def get_word_from_user(word)
puts "What is the #{word} to combine?"

Choose a reason for hiding this comment

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

word as a variable on its own is weird here. My own (admittedly fiddly) opinion is that it's not descriptive enough. Maybe word_num, or cardinal?

word = gets.chomp.to_s

Choose a reason for hiding this comment

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

I would also advise against reusing this variable again for a different purpose! Part of your job is to convey the semantic meaning of your code with variable names, so using word to mean both the cardinality of the word (first second etc.) and the word that we fetch from the user is confusing!

while word.length < 2 || word.include?(" ")
puts "Word must be at least 2 characters long, and must not contain any spaces."
word = gets.chomp.to_s
end
return word
end


def chop_first_word(first_word)
stop_index = first_word.length - 1

first_word.reverse.each_char do |letter|
if is_vowel?(letter)
return first_word[0...stop_index]
end
stop_index -= 1
end
stop_index = first_word.length
return first_word[0...stop_index]
end


def chop_second_word(second_word)

Choose a reason for hiding this comment

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

I bet you could find a clever way to combine chop_first_word and chop_second_word

start_index = 0

second_word.each_char do |letter|
if is_vowel?(letter)
return second_word[start_index..second_word.length]
end
start_index += 1
end
start_index = 0
return second_word[start_index..second_word.length]
end


def run_generator
first_word = get_word_from_user("first word")
second_word = get_word_from_user("second word")

first_half = chop_first_word(first_word)
second_half = chop_second_word(second_word)

Choose a reason for hiding this comment

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

This this beautiful! Great use of methods!


new_word = first_half + second_half

puts "#{first_word} + #{second_word} is #{new_word}"
end


run_generator
play_again = true

while play_again
puts "Wasn't that fun? if you would like to try again, please enter 'yes'."
responce = gets.chomp.to_s
if responce == "yes"
run_generator
else
play_again = false
end
end