【问题标题】:ruby scripts - (NameError) undefined local variable or method `null' for main:Objectruby 脚本 - (NameError) 未定义的局部变量或方法 `null' 用于 main:Object
【发布时间】:2018-07-09 13:06:23
【问题描述】:

我的代码

require "json"
require "erb"

flowvar = $workflowvar
path = 'src/main/resources/'+$workflowvar+'.drl'
rule = ""

File.open(path,"w") do |f|
    f.puts "package com.drools.demo\;"+"\n"+"import org.mule.MessageExchangePattern\;"+"\n"+"import com.drools.demo.cashliquidassets\;"+"\n"+"global org.mule.module.bpm.MessageService mule\;"+"\n"+
    "dialect \"mvel\""+"\n"+"dialect \"java\""+"\n"+"declare cashliquidassets"+"\n"+"@role\(\'event\'\)"+"\n"+"end"+"\n"
    f.close
end

def concateRule(attribute,val)
    if(val==null || val=="") 
            return "";
    end
    if(attribute != null)
            if (attribute == "taxonomy_code" || attribute == "parent_taxonomy_code" || attribute == "report_name")
                return "";
            end
    end

    if val.start_with('<>')
            return attribute+" != "+val[3,val.length].strip
    elsif val.start_with('>')
            return attribute+" > "+val
    elsif val.start_with('<')
            return attribute+" < "+val
    elsif val.include? ","
            return attribute+".contains("+val+"\)"
    else
        return attribute+" == "+ val
    end
end

json = JSON.parse($payload)
json.each do |hash1|
  hash1.keys.each do |key|
    hash1[key].each do |inner_hash,value|
    @inner_hash = inner_hash
    @values = value
        str = concateRule @inner_hash,$values
    end
  end

end

编译工作正常,但在运行时,我收到以下错误。任何建议


根异常堆栈跟踪:

org.jruby.exceptions.RaiseException: (NameError) undefined local main:Object 的变量或方法“null”

at RUBY.concateRule(<script>:15)
at RUBY.block in (root)(<script>:43)
at org.jruby.RubyHash.each(org/jruby/RubyHash.java:1350)
at RUBY.block in (root)(<script>:40)
at org.jruby.RubyArray.each(org/jruby/RubyArray.java:1735)
at RUBY.block in (root)(<script>:39)
at org.jruby.RubyArray.each(org/jruby/RubyArray.java:1735)
at RUBY.<main>(<script>:38)

【问题讨论】:

  • 尝试使用nil 而不是null
  • 谢谢。空问题已解决。现在我得到 org.jruby.exceptions.RaiseException: (NoMethodError) undefined method `start_with?'对于 9:Fixnum。我加了“?”上面的代码中缺少的
  • starts_withString 类的方法,根据错误使用整数,您需要先将其转换为字符串,尝试val.to_s.starts_with
  • @bgara 我认为这里真正的收获应该是让您学习如何自己调试代码,而不是一次发布一个错误。 (尤其是当错误消息如此清晰时。)显然,您的$payload(使用全局变量是不好的做法,但这是另一回事)包含数字和字符串。 start_with?String 上的一个方法。也许您可以做的是:val = val.to_sconcateRule 方法的顶部 - 从而无需在下面多次调用 to_s
  • 请注意,由于nil.to_s == "",这样做也无需检查下面一行中的val==nil

标签: ruby-on-rails ruby


【解决方案1】:

您需要使用nil 而不是null

所以,只需更换它。

【讨论】:

    【解决方案2】:

    根据上面 cmets 中的对话,我将如何编写该方法:

    def concat_rule(attribute, val)
      val = val.to_s
      if val == '' || ['taxonomy_code', 'parent_taxonomy_code', 'report_name'].include?(attribute)
        return ''
      end
    
      if val.start_with?('<>')
        "#{attribute} != #{val[3..-1].strip}"
      elsif val.start_with?('>')
        "#{attribute} > #{val}"
      elsif val.start_with?('<')
        "#{attribute} < #{val}"
      elsif val.include?(',')
        "#{attribute}.contains(#{val})"
      else
        "#{attribute} == #{val}"
      end
    end
    

    几点说明:

    • 使用 snake_case 方法名称和 2 个空格制表符,是 ruby​​ 社区中非常坚持的 style guide
    • 同样,你可以利用ruby的隐式return来缩短代码:方法结束时的最终值会自动返回。
    • 在此方法的顶部添加val = val.to_s 可以简化其余代码;无需重复转换为字符串或执行nil 检查。
    • 您可以使用 ruby​​ 的字符串插值 ("#{code-to-evaluate}") 语法作为定义字符串的更优雅的方式,而不是重复使用 + 进行连接。

    【讨论】:

    • 小错字start_with应该是start_with?
    • 糟糕,已修复,谢谢。我是通过复制 OP 的代码得到的。
    猜你喜欢
    • 2016-02-14
    • 2016-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多