-2

As a class exercise in my SkillCrush course we have to use the command line to return a user's birth path number and what it says about them. My first method will return the number when the second method is commented out but will not display the result of the second method?

def birth_path_number (birth_date)
birth_date = birth_date[0].to_i + birth_date[1].to_i + birth_date[2].to_i + birth_date[3].to_i + birth_date[4].to_i + birth_date[5].to_i + birth_date[6].to_i + birth_date[7].to_i
birth_date = birth_date.to_s
second_number = birth_date[0].to_i + birth_date[1].to_i
if second_number > 9 
        then birth_date = second_number[0].to_i + second_number[1].to_i
    else
        birth_date = second_number.to_i
    end
    puts birth_date
end

def number (birth_date)
  if birth_date == 1
    then display_one = "One is the leader. The number one indicates the ability to stand alone and is a strong vibration. Ruled by the Sun."
    elsif birth_date == 2
      then display_one = "This is the mediator and peace-lover. The number two indicates the desire for harmony. It is a gentle, considerate, and sensitive vibration. Ruled by the Moon."
      elsif birth_date == 3
        then display_one = "Number Three is a sociable, friendly, and outgoing vibration. Kind, positive, and optimistic, Three's enjoy life and have a good sense of humor. Ruled by Jupiter."
        elsif birth_date == 4
          then display_one = "This is the worker. Practical, with a love of detail, Fours are trustworthy, hard-working, and helpful. Ruled by Uranus."
          elsif birth_date == 5
            then display_one = "This is the freedom lover. The number five is an intellectual vibration. These are 'idea' people with a love of variety and the ability to adapt to most situations. Ruled by Mercury."
            elsif birth_date == 6
              then display_one = "This is the peace lover. The number six is a loving, stable, and harmonious vibration. Ruled by Venus."
              elsif birth_date == 7 
                then display_one = "This is the deep thinker. The number seven is a spiritual vibration. These people are not very attached to material things, are introspective, and generally quiet. Ruled by Neptune."
                elsif birth_date == 8
                  then display_one = "This is the manager. Number Eight is a strong, successful, and material vibration. Ruled by Saturn."
                  elsif birth_date == 9
                    then display_one = "This is the teacher. Number Nine is a tolerant, somewhat impractical, and sympathetic vibration. Ruled by Mars."
  end
    puts display_one
end

puts "Write your birthday in the format MMDDYYYY"
birth_date = gets
birth_path_number (birth_date)
number (birth_date
  • Maybe it's better to use a switch statement: https://stackoverflow.com/questions/948135/how-to-write-a-switch-statement-in-ruby – Hypenate Jan 28 '21 at 09:33
  • Your code is syntactically incorrect. This shouldn't even run. Please ensure you post the entire code correctly and format it properly. – idmean Jan 28 '21 at 09:37

1 Answers1

0

First, I'm just going to repeat your code with the formatting corrected. (Indentation is all over the place... Indentation is supposed to make the code easier to understand, not harder! If you're not 100% sure how indentation works, then use the auto-indentation feature of your chosen editor.)

def birth_path_number(birth_date)
  birth_date = birth_date[0].to_i + birth_date[1].to_i + birth_date[2].to_i + birth_date[3].to_i + birth_date[4].to_i + birth_date[5].to_i + birth_date[6].to_i + birth_date[7].to_i
  birth_date = birth_date.to_s
  second_number = birth_date[0].to_i + birth_date[1].to_i
  if second_number > 9
    then birth_date = second_number[0].to_i + second_number[1].to_i
  else
    birth_date = second_number.to_i
  end
  puts birth_date
end

def number(birth_date)
  if birth_date == 1
    then display_one = "One is the leader. The number one indicates the ability to stand alone and is a strong vibration. Ruled by the Sun."
  elsif birth_date == 2
    then display_one = "This is the mediator and peace-lover. The number two indicates the desire for harmony. It is a gentle, considerate, and sensitive vibration. Ruled by the Moon."
  elsif birth_date == 3
    then display_one = "Number Three is a sociable, friendly, and outgoing vibration. Kind, positive, and optimistic, Three's enjoy life and have a good sense of humor. Ruled by Jupiter."
  elsif birth_date == 4
    then display_one = "This is the worker. Practical, with a love of detail, Fours are trustworthy, hard-working, and helpful. Ruled by Uranus."
  elsif birth_date == 5
    then display_one = "This is the freedom lover. The number five is an intellectual vibration. These are 'idea' people with a love of variety and the ability to adapt to most situations. Ruled by Mercury."
  elsif birth_date == 6
    then display_one = "This is the peace lover. The number six is a loving, stable, and harmonious vibration. Ruled by Venus."
  elsif birth_date == 7
    then display_one = "This is the deep thinker. The number seven is a spiritual vibration. These people are not very attached to material things, are introspective, and generally quiet. Ruled by Neptune."
  elsif birth_date == 8
    then display_one = "This is the manager. Number Eight is a strong, successful, and material vibration. Ruled by Saturn."
  elsif birth_date == 9
    then display_one = "This is the teacher. Number Nine is a tolerant, somewhat impractical, and sympathetic vibration. Ruled by Mars."
  end
  puts display_one
end

puts "Write your birthday in the format MMDDYYYY"
birth_date = gets
birth_path_number(birth_date)
number(birth_date)

Now then, your main bug is that the number method is being called with the original input (e.g. "01282021"), not the "birth path number". Or to put this another way, contrary to the title of your question, the method is being called, but none of the if ... else if ... conditions are being met -- so display_one == nil, and an empty line is being printed to the terminal.

(It might also help if you actually told us what this arbitrary birth_path_number value is actually supposed to represent, otherwise I have no way of knowing if that part of the code is correct!)

Due to the way you've currently written the code, this is inevitable -- because the birth_path_number method doesn't actually return a value. Here is a minimal change to your code, to make it "work". (Note that I've renamed a couple of methods/variables, just to make it a tiny bit clearer what's going on.)

def birth_path_number(birth_date)
  birth_date = birth_date[0].to_i + birth_date[1].to_i + birth_date[2].to_i + birth_date[3].to_i + birth_date[4].to_i + birth_date[5].to_i + birth_date[6].to_i + birth_date[7].to_i
  birth_date = birth_date.to_s
  second_number = birth_date[0].to_i + birth_date[1].to_i
  if second_number > 9 
    then second_number[0].to_i + second_number[1].to_i
  else
    second_number.to_i
  end
end

def display_one(birth_path_number)
  if birth_path_number == 1
    then message = "One is the leader. The number one indicates the ability to stand alone and is a strong vibration. Ruled by the Sun."
  elsif birth_path_number == 2
    then message = "This is the mediator and peace-lover. The number two indicates the desire for harmony. It is a gentle, considerate, and sensitive vibration. Ruled by the Moon."
  elsif birth_path_number == 3
    then message = "Number Three is a sociable, friendly, and outgoing vibration. Kind, positive, and optimistic, Three's enjoy life and have a good sense of humor. Ruled by Jupiter."
  elsif birth_path_number == 4
    then message = "This is the worker. Practical, with a love of detail, Fours are trustworthy, hard-working, and helpful. Ruled by Uranus."
  elsif birth_path_number == 5
    then message = "This is the freedom lover. The number five is an intellectual vibration. These are 'idea' people with a love of variety and the ability to adapt to most situations. Ruled by Mercury."
  elsif birth_path_number == 6
    then message = "This is the peace lover. The number six is a loving, stable, and harmonious vibration. Ruled by Venus."
  elsif birth_path_number == 7 
    then message = "This is the deep thinker. The number seven is a spiritual vibration. These people are not very attached to material things, are introspective, and generally quiet. Ruled by Neptune."
  elsif birth_path_number == 8
    then message = "This is the manager. Number Eight is a strong, successful, and material vibration. Ruled by Saturn."
  elsif birth_path_number == 9
    then message = "This is the teacher. Number Nine is a tolerant, somewhat impractical, and sympathetic vibration. Ruled by Mars."
  end
  puts message
end

puts "Write your birthday in the format MMDDYYYY"
birth_date = gets

birth_path_number = birth_path_number(birth_date)
puts birth_path_number

display_one(birth_path_number)

And finally, here is a slightly bigger change to your code -- I'm not massively changing how it actually works, just trying to make it a bit simpler and cleaner:

def birth_path_number(birth_date)
  # Sum of digits
  sum1 = birth_date.chars.map(&:to_i).sum
  # Sum of digits again (in case the previous sum is greater than 9)
  sum2 = sum1.digits.sum
  # Sum of digits again (in case the previous sum is still greater than 9)
  sum2.digits.sum
end

def message_one(birth_path_number)
  case birth_path_number 
  when 1
    "One is the leader. The number one indicates the ability to stand alone and is a strong vibration. Ruled by the Sun."
  when 2
    "This is the mediator and peace-lover. The number two indicates the desire for harmony. It is a gentle, considerate, and sensitive vibration. Ruled by the Moon."
  when 3
    "Number Three is a sociable, friendly, and outgoing vibration. Kind, positive, and optimistic, Three's enjoy life and have a good sense of humor. Ruled by Jupiter."
  when 4
    "This is the worker. Practical, with a love of detail, Fours are trustworthy, hard-working, and helpful. Ruled by Uranus."
  when 5
    "This is the freedom lover. The number five is an intellectual vibration. These are 'idea' people with a love of variety and the ability to adapt to most situations. Ruled by Mercury."
  when 6
    "This is the peace lover. The number six is a loving, stable, and harmonious vibration. Ruled by Venus."
  when 7
    "This is the deep thinker. The number seven is a spiritual vibration. These people are not very attached to material things, are introspective, and generally quiet. Ruled by Neptune."
  when 8
    "This is the manager. Number Eight is a strong, successful, and material vibration. Ruled by Saturn."
  when 9
    "This is the teacher. Number Nine is a tolerant, somewhat impractical, and sympathetic vibration. Ruled by Mars."
  else
    raise "Error: Unknown birth path number #{birth_path_number}"
  end
end
    
puts "Write your birthday in the format MMDDYYYY"
birth_date = gets.chomp
    
birth_path_number = birth_path_number(birth_date)
puts birth_path_number
    
puts message_one(birth_path_number)
Tom Lord
  • 27,404
  • 4
  • 50
  • 77
  • Thank you so much, Tom! I am a Ruby student in the very beginning phases so your patience and help with my formatting is much appreciated! Funnily enough, my formatting was more like yours initially and a lecturer told me to change it to that triangular indentation but all sorted now. This does all work, it was the names of my arguments and methods that were not matched up resulting in the conditions not being met. I really, really appreciate and I hope you are doing well! G – Gareth Grey Feb 09 '21 at 11:32