首页 > 解决方案 > RUBY TERMINAL:为什么终端运行第一种方法而不是第二种方法?

问题描述

作为我的 SkillCrush 课程中的一个课堂练习,我们必须使用命令行来返回用户的出生路径编号以及它对他们的描述。我的第一种方法会在第二种方法被注释掉时返回数字但不会显示第二种方法的结果?

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

标签: rubyterminal

解决方案


首先,我将重复您的代码,格式已更正。(缩进无处不在......缩进应该让代码更容易理解,而不是更难!如果你不是 100% 确定缩进是如何工作的,那么使用你选择的编辑器的自动缩进功能。)

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)

现在,您的主要错误是number使用原始输入(例如"01282021")调用该方法,而不是“出生路径编号”。或者换一种说法,与您的问题的标题相反,正在调用该方法但没有满足任何if ... else if ...条件 - 所以display_one == nil,并且正在向终端打印一个空行。

(如果你真的告诉我们这个任意birth_path_number值实际上应该代表什么,这也可能会有所帮助,否则我无法知道那部分代码是否正确!)

由于您当前编写代码的方式,这是不可避免的——因为该birth_path_number方法实际上并没有返回值。这是对您的代码的最小更改,以使其“工作”。(请注意,我已经重命名了几个方法/变量,只是为了让它更清楚发生了什么。)

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)

最后,这里对您的代码进行了稍微大一点的更改——我并没有大规模更改它的实际工作方式,只是想让它更简单、更清晰:

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)

推荐阅读