0

I am trying to ask the user for a name so we can create a fake name.

Fake name will:

  • Swap the first and last name

  • Change all the vowels (a,e,i,o,u) to next vowel, So 'a' would become 'e','u' would become 'a'

  • Change all the consonants to (besides the vowels) to the next consonant in the alphabet, So 'd' would become 'f', 'n' would become 'p'

I am unable to get the first and last name capitalized at the end of the program. Any advice on how to do this?

I am also trying to Use a data structure to store the fake names as they are entered. When the user exits the program, iterate through the data structure and print all of the data the user entered. A sentence like "Vussit Gimodoe is actually Felicia Torres" or "Felicia Torres is also known as Vussit Gimodoe

# ---- Swap First and Last Name ----

def name_swap(name)
  name.split.reverse.join(' ')
end

# ---- Change all the vowels ----

def vowel_change(name)
  vowels = ['a', 'e', 'i', 'o', 'u']
  name = name.downcase.split('')
  new_name = name.map do |vow|
    if vowels.include?(vow)
       vowels.rotate(1)[vowels.index(vow)]
     elsif vowels == 'u'
      vowels.replace('a')
     else
      vow
     end
   end
  new_name.join
end

# ---- Change the constant ----

def constant_change(name)
  constants = ['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z']
  name = name.downcase.split('')
  new_name = name.map do |letter|
    if constants.include?(letter)
       constants.rotate(1)[constants.index(letter)]
     elsif constants == 'z'
        constants.replace('b')
     else
      letter
     end
   end
     new_name.join.capitalize!
end


# ---- User Interface and Data Storage----
valid_input = false
agents ={}
user_input = ""
secret_name = ""

until valid_input
  puts "Hello Secret Agent, to begin please enter the name you would like to change. When you are finished, type 'quit' !"
  user_input = gets.chomp.downcase
    if user_input == "quit"
       puts "Thank You!"
       valid_input = true
     else
      secret_name = name_swap(vowel_change(constant_change(user_input)))
       puts "Your secret name is #{secret_name} "
      end
      agents[user_input] = secret_name
    end


agents.each do |name, scramble|
  puts"#{name} is also known #{scramble}"
end

The problem is when I return the final agent name the name is lower case and I need both the first name and last name capitalized. It is also taking quit to exit the until loop as a name.

user229044
  • 232,980
  • 40
  • 330
  • 338
SG17
  • 41
  • 10
  • Tip: You can do things like `vow.match(/[aeiou]/)` or `vowels = 'aeiou'.chars.to_a` to either use a regular expression for quick matching, or to quickly make an array. – tadman Oct 15 '16 at 01:57
  • @tadman i do not understand? – SG17 Oct 15 '16 at 02:01
  • The question is a bit unclear (it doesn't describe in what way the code does not work), and also broad (there's something about capitalization, and also something about using a data structure). I didn't vote to close it, because you are taking the effort to make a good question (thank you!), but the question does need improvement. – Wayne Conrad Oct 15 '16 at 02:32
  • 1
    Please boil you question down to *one* question, with the smallest amount of code that reproduces your issue. Part of the requirements of asking a question here is that you should have already done enough debugging to isolate your problem and be able to reproduce it. – user229044 Oct 15 '16 at 12:53

2 Answers2

2

Have a look at this: https://ruby-doc.org/core-2.2.0/String.html#method-i-capitalize-21

You could do this before you print the secret name:

output = output.split.each { |name| name.capitalize! }.join(' ')

and the same for the original name:

original_name = original_name.split.each { |name| name.capitalize! }.join(' ')

Examples from Ruby docs:

a = "hello"
a.capitalize!   #=> "Hello"
a               #=> "Hello"
a.capitalize!   #=> nil

Or use Rails' titleize method if you are using Rails.

http://apidock.com/rails/String/titleize

bralley
  • 36
  • 5
  • still does not work – SG17 Oct 14 '16 at 20:41
  • Sorry I fixed it. That should work now. – bralley Oct 14 '16 at 20:52
  • rb:62: syntax error, unexpected tSTRING_DEND, expecting keyword_end ...ch do |name| name.capitalize! }.join(' ') – SG17 Oct 14 '16 at 21:19
  • Fixed AGAIN, sorry about that. – bralley Oct 14 '16 at 22:30
  • I cleaned up my code and added a little more context on what I am trying to do. I don't understand why I cant do it :( sorry but i am new to coding – SG17 Oct 14 '16 at 23:55
  • I actually did this assignment so I know exactly what you're trying to do! But no problem! Is capitalization the only problem you're having? – bralley Oct 15 '16 at 01:05
  • yes. and when i print the final hash, keys and values it prints out "quit' as the key and whatever was the last user input. – SG17 Oct 15 '16 at 01:59
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/125763/discussion-between-sg17-and-bralley). – SG17 Oct 15 '16 at 02:08
0

In rails, you have a method called titleize

that said, give this a try

output = name_swap(vowel_change(constant_change(original_name))).titleize
original_name = original_name.titleize

Reference Answer: https://stackoverflow.com/a/15005638/2398387

Community
  • 1
  • 1
Belfordz
  • 630
  • 6
  • 13