Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/58.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/Rails:在运行时确定比较器?_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails Ruby/Rails:在运行时确定比较器?

Ruby on rails Ruby/Rails:在运行时确定比较器?,ruby-on-rails,ruby,Ruby On Rails,Ruby,我正在从事一个Rails项目,该项目对数据库执行查询,然后将结果与用户指定的条件进行比较。这个错误检测过程的要点可以总结如下: case error_operator when '<' if results.row_count < desired_row_count then # do error-related stuff else # do other stuff end when '<=' if results.r

我正在从事一个Rails项目,该项目对数据库执行查询,然后将结果与用户指定的条件进行比较。这个错误检测过程的要点可以总结如下:

case error_operator
  when '<'
    if results.row_count < desired_row_count
      then # do error-related stuff 
      else # do other stuff
    end
  when '<='
    if results.row_count <= desired_row_count
      then # do error-related stuff 
      else # do other stuff
    end
  when '>'
    if results.row_count > desired_row_count
      then # do error-related stuff 
      else # do other stuff
    end
  when '>='
    if results.row_count >= desired_row_count
      then # do error-related stuff 
      else # do other stuff
    end
  when '!='
    if results.row_count != desired_row_count
      then # do error-related stuff 
      else # do other stuff
    end
  when '=='
    if results.row_count == desired_row_count
      then # do error-related stuff 
      else # do other stuff
    end
  end
error_操作符变量作为任务对象的属性以字符串形式存储在Rails中,因此每个任务可能有不同的比较器。有没有办法让Ruby认识到给定的字符串可以作为比较器进行解析?如果可以选择的话,我也愿意以不同的方式存储给定任务的比较器。我没有写原始代码,所以我没有嫁给它


谢谢

对你需要什么感到困惑。将字符串转换为符号并在Integer.instance_方法中查找它是否不起作用,因为这是row_count返回值的类?在了解了Ruby中的一切是如何成为对象之后,我能够调用results.row_count.methoderror_operator.desired_row_count并使其起作用。现在,比较是使用运行时确定的比较器完成的。如果您只需要将字符串映射到方法调用,您可以通过将字符串和参数发送到所讨论的整数并跳过实例化方法来进一步缩短代码:results.row\u count.senderError\u operator,desired\u row\u count。在Ruby中,您可以将传统的方法调用视为向对象发送方法名+参数的语法糖。