【问题标题】:RUBY TERMINAL: Why is Terminal running the first method and not the second?RUBY TERMINAL:为什么终端运行第一种方法而不是第二种方法?
【发布时间】:2021-05-02 04:08:32
【问题描述】:

在我的 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

【问题讨论】:

  • 或许最好使用switch语句:stackoverflow.com/questions/948135/…
  • 您的代码在语法上不正确。这甚至不应该运行。请确保您正确发布整个代码并正确格式化。

标签: ruby terminal


【解决方案1】:

首先,我将重复您的代码格式已更正。 (缩进无处不在...缩进应该让代码更容易理解,而不是更难!如果您不能 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"),而不是“出生路径编号”。或者换一种说法,与您的问题的标题相反,方法 is 被调用,但没有满足 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)

【讨论】:

  • 非常感谢你,汤姆!我是刚开始阶段的 Ruby 学生,因此非常感谢您对我的格式化的耐心和帮助!有趣的是,我的格式最初更像你的格式,一位讲师告诉我将其更改为三角形缩进,但现在全部排序。这一切都有效,是我的参数和方法的名称不匹配导致条件不满足。我真的,真的很感激,我希望你一切都好! G
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-17
  • 2022-01-20
  • 2023-03-16
相关资源
最近更新 更多