Ruby:作为字符串的比较运算符
我正在开发一个Rails应用程序,它允许用户在完成任务后检查任务,我正在开发一种方法,该方法将返回过期任务和即将到来的任务 以下是我的用户模型中的方法:Ruby:作为字符串的比较运算符,ruby,operators,Ruby,Operators,我正在开发一个Rails应用程序,它允许用户在完成任务后检查任务,我正在开发一种方法,该方法将返回过期任务和即将到来的任务 以下是我的用户模型中的方法: def task_notification(notification_type = "past") return false if tasks.empty? current_month = Time.now.month tasks.where(classification: classification).sele
def task_notification(notification_type = "past")
return false if tasks.empty?
current_month = Time.now.month
tasks.where(classification: classification).select { |task| task.due_date < current_month }
end
def任务通知(通知类型=“过去”)
如果tasks.empty返回false?
当前月份=Time.now.month
任务。其中(分类:分类)。选择{|任务|任务.到期日<当前月}
结束
我基本上是在尝试这样做:
operator = notification_type.eql?("past") ? "<" : ">"
tasks.where(classification: classification).select { |task| task.due_date operator current_month }
operator=notification_type.eql?(“过去”)?""
tasks.where(分类:classification)。选择{| task | task.due_date operator current_month}
显然,上述方法不起作用,我只是想知道如何使其起作用。使用
使用
让我试试!!谢谢这不应该是
操作符吗?to_-sym
?@steenslag:Object#send
很聪明,可以独立完成。很有魅力!!非常感谢!让我试试!!谢谢这不应该是操作符吗?to_-sym
?@steenslag:Object#send
很聪明,可以独立完成。很有魅力!!非常感谢!
task.due_date.send(operator, current_month)