Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 用方法简化我的代码?_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 用方法简化我的代码?

Ruby on rails 用方法简化我的代码?,ruby-on-rails,ruby,Ruby On Rails,Ruby,我是Ruby的新手,我正在创建小预算助手 我知道有一种方法可以进一步简化这段代码,但我似乎不知道需要创建什么方法。我在重复需要,想要,保存和五十、三十、二十等等: puts "What's your annual income?" annual_income = gets.to_i weeks = 52.1775 monthly_income = ((annual_income / weeks) * 2) weekly_income = (annual_income / weeks) nee

我是Ruby的新手,我正在创建小预算助手

我知道有一种方法可以进一步简化这段代码,但我似乎不知道需要创建什么方法。我在重复
需要
想要
保存
和五十、三十、二十等等:

puts "What's your annual income?"
annual_income = gets.to_i

weeks = 52.1775
monthly_income = ((annual_income / weeks) * 2)
weekly_income = (annual_income / weeks) 
needs = 0.5
wants = 0.3
save = 0.2

def calc_amount(income, expense)
  sprintf('%.2f',(income * expense))
end

# Monthly 
fifty_percent_monthly = calc_amount(monthly_income, needs)
puts "You should spend no more than $#{fifty_percent_monthly} on 'Needs' a month."

thirty_percent_monthly = calc_amount(monthly_income, wants)
puts "You should spend no more than $#{thirty_percent_monthly} on 'Wants' a month."

twenty_percent_monthly = calc_amount(monthly_income, save)
puts "You should save $#{twenty_percent_monthly} a month."

# Each paycheck
fifty_percent_weekly = calc_amount(weekly_income, needs)
puts "You should spend no more than $#{fifty_percent_weekly} on 'Needs' each paycheck."

thirty_percent_weekly = calc_amount(weekly_income, wants)
puts "You should spend no more than $#{thirty_percent_weekly} on 'Wants' each paycheck."

twenty_percent_weekly = calc_amount(weekly_income, save)
puts "You should save $#{twenty_percent_weekly} each paycheck."

# Total spent each year
yearly_needs = calc_amount(annual_income, needs)
puts "You'll be spending $#{yearly_needs} on 'Needs' each year."

yearly_wants = calc_amount(annual_income, wants)
puts "You'll be spending $#{yearly_wants} on 'Wants' each year."

yearly_savings = calc_amount(annual_income, save
puts "Congrats! Your total savings each year will be $#{yearly_savings}"

基本上,您希望在每种类型的时间段上循环,然后在该时间段内循环预算中的每一项。这里有一个简单的方法可以实现:

puts "What's your annual income?"
annual_income = gets.to_i

budget = {
  :needs => 0.5,
  :wants => 0.3,
  :save => 0.2,
}

periods = {
  'weekly' => 52.1775,
  'monthly' => 12,
  'yearly' => 1,
}

periods.each do |period_name, periods_per_year|
  budget.each do |line_item_name, line_item_fraction|
    amount = annual_income.to_f/periods_per_year * line_item_fraction
    puts "You should spend %0.2f on '%s' %s" % [amount, line_item_name, period_name]
  end
end
输出结果与您的不完全相同,但可以正常工作。以下是输入1000后得到的结果:

You should spend 9.58 on 'needs' weekly
You should spend 5.75 on 'wants' weekly
You should spend 3.83 on 'save' weekly
You should spend 41.67 on 'needs' monthly
You should spend 25.00 on 'wants' monthly
You should spend 16.67 on 'save' monthly
You should spend 500.00 on 'needs' yearly
You should spend 300.00 on 'wants' yearly
You should spend 200.00 on 'save' yearly

基本上,您希望在每种类型的时间段上循环,然后在该时间段内循环预算中的每一项。这里有一个简单的方法可以实现:

puts "What's your annual income?"
annual_income = gets.to_i

budget = {
  :needs => 0.5,
  :wants => 0.3,
  :save => 0.2,
}

periods = {
  'weekly' => 52.1775,
  'monthly' => 12,
  'yearly' => 1,
}

periods.each do |period_name, periods_per_year|
  budget.each do |line_item_name, line_item_fraction|
    amount = annual_income.to_f/periods_per_year * line_item_fraction
    puts "You should spend %0.2f on '%s' %s" % [amount, line_item_name, period_name]
  end
end
输出结果与您的不完全相同,但可以正常工作。以下是输入1000后得到的结果:

You should spend 9.58 on 'needs' weekly
You should spend 5.75 on 'wants' weekly
You should spend 3.83 on 'save' weekly
You should spend 41.67 on 'needs' monthly
You should spend 25.00 on 'wants' monthly
You should spend 16.67 on 'save' monthly
You should spend 500.00 on 'needs' yearly
You should spend 300.00 on 'wants' yearly
You should spend 200.00 on 'save' yearly

基本上,您希望在每种类型的时间段上循环,然后在该时间段内循环预算中的每一项。这里有一个简单的方法可以实现:

puts "What's your annual income?"
annual_income = gets.to_i

budget = {
  :needs => 0.5,
  :wants => 0.3,
  :save => 0.2,
}

periods = {
  'weekly' => 52.1775,
  'monthly' => 12,
  'yearly' => 1,
}

periods.each do |period_name, periods_per_year|
  budget.each do |line_item_name, line_item_fraction|
    amount = annual_income.to_f/periods_per_year * line_item_fraction
    puts "You should spend %0.2f on '%s' %s" % [amount, line_item_name, period_name]
  end
end
输出结果与您的不完全相同,但可以正常工作。以下是输入1000后得到的结果:

You should spend 9.58 on 'needs' weekly
You should spend 5.75 on 'wants' weekly
You should spend 3.83 on 'save' weekly
You should spend 41.67 on 'needs' monthly
You should spend 25.00 on 'wants' monthly
You should spend 16.67 on 'save' monthly
You should spend 500.00 on 'needs' yearly
You should spend 300.00 on 'wants' yearly
You should spend 200.00 on 'save' yearly

基本上,您希望在每种类型的时间段上循环,然后在该时间段内循环预算中的每一项。这里有一个简单的方法可以实现:

puts "What's your annual income?"
annual_income = gets.to_i

budget = {
  :needs => 0.5,
  :wants => 0.3,
  :save => 0.2,
}

periods = {
  'weekly' => 52.1775,
  'monthly' => 12,
  'yearly' => 1,
}

periods.each do |period_name, periods_per_year|
  budget.each do |line_item_name, line_item_fraction|
    amount = annual_income.to_f/periods_per_year * line_item_fraction
    puts "You should spend %0.2f on '%s' %s" % [amount, line_item_name, period_name]
  end
end
输出结果与您的不完全相同,但可以正常工作。以下是输入1000后得到的结果:

You should spend 9.58 on 'needs' weekly
You should spend 5.75 on 'wants' weekly
You should spend 3.83 on 'save' weekly
You should spend 41.67 on 'needs' monthly
You should spend 25.00 on 'wants' monthly
You should spend 16.67 on 'save' monthly
You should spend 500.00 on 'needs' yearly
You should spend 300.00 on 'wants' yearly
You should spend 200.00 on 'save' yearly


完美的这足以让我习惯它的工作原理。+1,做得很好。除了出色的重构之外,使用String的
%
而不是传统的
sprintf
是Ruby的惯用用法。我看到您唯一遗漏的是在值前面使用了一个美元符号,但这可以被指定为一个常量,然后替换为格式字符串,只需稍加调整即可使代码适用于任何货币。太棒了!这足以让我习惯它的工作原理。+1,做得很好。除了出色的重构之外,使用String的
%
而不是传统的
sprintf
是Ruby的惯用用法。我看到您唯一遗漏的是在值前面使用了一个美元符号,但这可以被指定为一个常量,然后替换为格式字符串,只需稍加调整即可使代码适用于任何货币。太棒了!这足以让我习惯它的工作原理。+1,做得很好。除了出色的重构之外,使用String的
%
而不是传统的
sprintf
是Ruby的惯用用法。我看到您唯一遗漏的是在值前面使用了一个美元符号,但这可以被指定为一个常量,然后替换为格式字符串,只需稍加调整即可使代码适用于任何货币。太棒了!这足以让我习惯它的工作原理。+1,做得很好。除了出色的重构之外,使用String的
%
而不是传统的
sprintf
是Ruby的惯用用法。我看到您唯一遗漏的是在值前面使用了一个美元符号,但这可以被指定为一个常量,然后替换为格式字符串,只需稍加调整即可使代码适用于任何货币。解决此问题的更好位置可能是。好的!非常感谢。下次我一定会发到那里。这个问题似乎离题了,因为它似乎属于codereview.stackexchange.comA。这个问题最好的地方可能是。好的!非常感谢。下次我一定会发到那里。这个问题似乎离题了,因为它似乎属于codereview.stackexchange.comA。这个问题最好的地方可能是。好的!非常感谢。下次我一定会发到那里。这个问题似乎离题了,因为它似乎属于codereview.stackexchange.comA。这个问题最好的地方可能是。好的!非常感谢。下次我一定会在那里发帖。这个问题似乎离题了,因为它似乎属于codereview.stackexchange.com