Ruby:错误:当我提供正确数量的参数时,错误的参数数量(给定1,应为0)

时间:2018-11-19 21:54:22

标签: ruby class methods arguments

这是课程文件:

class HealthProfile
    attr_reader :firstName, :lastName, :gender, :birthYear, :currentYear, :height, :weight
    attr_writer :firstName, :lastName, :gender, :birthYear, :currentYear, :height, :weight

def initialize(firstName, lastName, gender, birthYear, currentYear, height, weight)
    @firstName = firstName
    @lastName = lastName
    @gender = gender
    @birthYear = birthYear
    @currentYear = currentYear
    @height = height
    @weight = weight
end


def userAge(currentYear, birthYear)
    age = currentYear - birthYear
    return age
end


def maxHeartRate(age) #The maximum heart rate is computed as 220 minus age in years
    maxhr = 220 - age
    return maxhr
end

def targetHeartRateRange(maxhr) #The target-heart-rate is a range that is 50-89% of the maximum heart rate
    lowtarget = 0.50 * maxhr
    hightarget = 0.89 * maxhr
    return lowtarget, hightarget
end


def BMI(weight, height) #(weight:lbs * 703) / (height:inches * height:inches)
    bmi = (weight * 703) / (height * height)
    return bmi
end

def displayHealthProfile(firstName, lastName, gender, age, height, weight, maxhr, lowtarget, hightarget, bmi)
    puts ("HEALTH PROFILE FOR: #{firstName lastName}")
    puts ("***********************************")
    puts
    puts ("Gender: #{gender}")
    puts ("Age: #{age}")
    puts ("Height (in inches): #{height}")
    puts ("Weight (in pounds): #{weight}")
    puts ("Maximum heart rate: #{maxhr}")
    puts
    puts
    puts ("Target heart rate range")
    puts ("*************************")
    puts
    puts ("Minimum: #{lowtarget}")
    puts ("Maximum: #{hightarget}")
    puts ("BMI: #{bmi}")
    puts
    puts
    puts ("BMI VALUES")
    puts ("************")
    puts
    puts ("Underweight: less than 18.5")
    puts ("Normal: between 18.5 and 24.9")
    puts ("Overweight: between 25 and 29.9")
    puts ("Obese: 30 or greater")
end
end

以下是主要文件代码:

require_relative 'lab11class.rb'
require 'date'

currentYear = Date.today.year
currentYear = currentYear.to_i()

print ("Enter your First Name: ")
firstName = gets()
print ("Enter your Last Name: ")
lastName = gets()
print ("Enter your gender (Male/Female): ")
gender = gets()
print ("Enter your year of birth: ")
birthYear = gets().to_i()
print ("Enter your height in inches: ")
height = gets().to_i()
print ("Enter your weight in pounds: ")
weight = gets().to_i()

hp = HealthProfile.new(firstName, lastName, gender, birthYear, currentYear,     height, weight)
age = hp.userAge(currentYear, birthYear)
maxhr = hp.maxHeartRate(age)
lowtarget, hightarget = hp.targetHeartRateRange(maxhr)
bmi = hp.BMI(weight, height)
hp.displayHealthProfile(firstName, lastName, gender, age, height, weight, maxhr, lowtarget, hightarget, bmi)

这是完整错误

lab11class.rb:40:in `displayHealthProfile': wrong number of arguments (given 1, expected 0) (ArgumentError)

问题显然出在displayHealthProfile方法上。它应该期望有10个参数,但是当我在主文件中输入所有10个参数时,就会出现错误。 但是,如果我从主文件中的调用中删除一个或多个参数,则会收到错误消息,但提示(给定9,预期为10),如果要删除更多则以此类推。 我想念什么吗?任何帮助表示赞赏。

更新 我已经修复它,并且程序可以正常工作。方法根本没有问题,正确定义了displayHealthProfile。问题在于firstName和lastName变量是该方法下使用的第一个变量。这两个变量使用换行符进行了解析,而换行符在方法调用中效果不佳。所以这是修复它的更改。

原始

puts ("HEALTH PROFILE FOR: #{firstName lastName}")

更改为:

puts ("HEALTH PROFILE FOR: #{firstName.chomp} #{lastName.chomp}")

感谢您的回复!

2 个答案:

答案 0 :(得分:1)

在第2行中,您将firstName定义为带有零参数的方法:

attr_reader :firstName

(这就是阅读器方法:不带参数的方法。)

在这里,您使用一个参数调用firstName

puts ("HEALTH PROFILE FOR: #{firstName lastName}")

Ruby使您可以省略参数列表周围的括号,这意味着

firstName lastName

(在字符串替换中)与

相同
self.firstName(lastName)

您可能的意思是:

puts ("HEALTH PROFILE FOR: #{firstName} #{lastName}")
#                                     ↑ ↑↑
  

用换行符解析了这两个变量,它们在方法调用中并不适合。

这与字符串中的换行符无关。字符串的内容不可能导致此错误。该错误是由于您使用一个参数调用firstName而将其定义为零而引起的。

答案 1 :(得分:-1)

在health_profile.rb

class HealthProfile
  def initialize(firstName, lastName, gender, birthYear, currentYear, height, weight)
    @firstName = firstName
    @lastName = lastName
    @gender = gender
    @birthYear = birthYear
    @currentYear = currentYear
    @height = height
    @weight = weight
  end

  def userAge
    @currentYear - @birthYear
  end

  def maxHeartRate #The maximum heart rate is computed as 220 minus age in years
    220 - userAge
  end

  def targetHeartRateRange #The target-heart-rate is a range that is 50-89% of the maximum heart rate
    maxhr = maxHeartRate
    lowtarget = 0.50 * maxhr
    hightarget = 0.89 * maxhr
    [lowtarget, hightarget]
  end

  def bmi #(weight:lbs * 703) / (height:inches * height:inches)
    (@weight * 703) / (@height * @height)
  end

  def displayHealthProfile
    lowtarget, hightarget = targetHeartRateRange

    puts "HEALTH PROFILE FOR: #{@firstName.chomp} #{@lastName}"
    puts "***********************************"
    puts
    puts "Gender: #{@gender}"
    puts "Age: #{userAge}"
    puts "Height (in inches): #{@height}"
    puts "Weight (in pounds): #{@weight}"
    puts "Maximum heart rate: #{maxHeartRate}"
    puts
    puts
    puts "Target heart rate range"
    puts "*************************"
    puts
    puts "Minimum: #{lowtarget}"
    puts "Maximum: #{hightarget}"
    puts "BMI: #{bmi}"
    puts
    puts
    puts "BMI VALUES"
    puts "************"
    puts
    puts "Underweight: less than 18.5"
    puts "Normal: between 18.5 and 24.9"
    puts "Overweight: between 25 and 29.9"
    puts "Obese: 30 or greater"
  end
end

在main.rb

require_relative 'health_profile.rb'
require 'date'

currentYear = Date.today.year
currentYear = currentYear.to_i()

print ("Enter your First Name: ")
firstName = gets()
print ("Enter your Last Name: ")
lastName = gets()
print ("Enter your gender (Male/Female): ")
gender = gets()
print ("Enter your year of birth: ")
birthYear = gets().to_i()
print ("Enter your height in inches: ")
height = gets().to_i()
print ("Enter your weight in pounds: ")
weight = gets().to_i()

hp = HealthProfile.new(firstName, lastName, gender, birthYear, currentYear, height, weight)
hp.displayHealthProfile

然后

ruby main.rb