如何计算方法中的方法 [英] How to count a method within a method

查看:152
本文介绍了如何计算方法中的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们创建一个名为 days_left_in_current_level 的新方法,我们需要在那里放一些,以便我们可以计算 current_level ?

If we create a new method called days_left_in_current_level, what would we need to put in there so that we can count how many days are left in the current_level?

habit.rb

def current_level
  return 0 unless date_started

  def committed_wdays
    committed.map do |day|
      Date::ABBR_DAYNAMES.index(day.titleize)
    end
  end

  def n_days
    ((date_started.to_date)..Date.today).count do |date| 
      committed_wdays.include? date.wday
    end - self.real_missed_days
  end     

  case n_days   # 1 - 6 represent the different levels  
    when 0..9
      1
    when 10..24
      2
    when 25..44
      3
    when 45..69
      4
    when 70..99
      5
    else
      6
  end
end

如果您需要进一步的解释或代码,请告诉我们(这里是 Gist

Please let me know if you need further explanation or code (here's the Gist of it).

推荐答案

def days_left_in_current_level

  def n_days
    ((date_started.to_date)..Date.today).count do |date| 
      committed_wdays.include? date.wday
    end - self.real_missed_days
  end     

    case n_days   
  when 0..9
    10-n_days
  when 10..24
    25-n_days
  when 25..44
    45-n_days
  when 45..69
    70-n_days
  when 70..99
    100-n_days
  else
    0 # No end
end
end

这篇关于如何计算方法中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆