bgara
bgara

Reputation: 143

ruby scripts - (NameError) undefined local variable or method `null' for main:Object

My code

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

Compile is working fine, but in runtime, I am getting this following error. Any suggestions


Root Exception stack trace:

org.jruby.exceptions.RaiseException: (NameError) undefined local variable or method `null' for main:Object

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)

Upvotes: 2

Views: 5701

Answers (2)

Tom Lord
Tom Lord

Reputation: 28305

Following the conversation in the comments above, here is how I would write the method:

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

A few notes:

  • Using snake_case method names and 2 space tabs, is a very strongly adhered to style guide in the ruby community.
  • Similarly, you can make use of ruby's implicit return, to shorten the code: The final value at the end of a method is returned automatically.
  • Adding val = val.to_s to the top of this method simplifies the rest of the code; eliminating the need to repeatedly convert to a string or perform nil checks.
  • You can use ruby's string interpolation ("#{code-to-evaluate}") syntax as a more elegant way to define strings than repeated use of + for concatenation.

Upvotes: 0

Bogdan
Bogdan

Reputation: 209

You need to use nil instead of null.

So, just replace it.

Upvotes: 6

Related Questions