# Unexpected error in ruby code in logstash

**URL:** <https://discuss.elastic.co/t/unexpected-error-in-ruby-code-in-logstash/70264>\
**Category:** Logstash\
**Created:** [December 30, 2016, 10:37am UTC](https://discuss.elastic.co/t/unexpected-error-in-ruby-code-in-logstash/70264 "2016-12-30T10:37:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ANU\_SARA\_VARGHESE](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anu_sara_varghese/32/51487_2.png) [@ANU\_SARA\_VARGHESE](https://discuss.elastic.co/u/ANU_SARA_VARGHESE)\
**Post date:** [December 30, 2016, 10:37am UTC](https://discuss.elastic.co/t/unexpected-error-in-ruby-code-in-logstash/70264/1 "2016-12-30T10:37:24Z")

</div>

Hi,  
An unexpected error in occuring for the following ruby code in logstash filter.

_**SyntaxError: (ruby filter code):5: syntax error, unexpected tRCURLY**_  
_\*\* }\*\*_

Following is the ruby code:

ruby {  
code =\> ' allscores = event["score"]  
if allscores.length != '1'  
t = allscores.inject(:+)  
event["total\_score"] = t  
'  
add\_tag =\> [sum\_score]  
}

**The error is as follows:**

SyntaxError: (ruby filter code):5: syntax error, unexpected tRCURLY  
}  
^  
eval at org/jruby/RubyKernel.java:1079  
register at /opt/logstash/vendor/bundle/jruby/1.9/gems/logstash-filter-ruby-2.0.5/lib/logstash/filters/ruby.rb:29  
start\_workers at /opt/logstash/vendor/bundle/jruby/1.9/gems/logstash-core-2.2.4-java/lib/logstash/pipeline.rb:174  
each at org/jruby/RubyArray.java:1613  
start\_workers at /opt/logstash/vendor/bundle/jruby/1.9/gems/logstash-core-2.2.4-java/lib/logstash/pipeline.rb:174  
run at /opt/logstash/vendor/bundle/jruby/1.9/gems/logstash-core-2.2.4-java/lib/logstash/pipeline.rb:126  
execute at /opt/logstash/vendor/bundle/jruby/1.9/gems/logstash-core-2.2.4-java/lib/logstash/agent.rb:210  
run at /opt/logstash/vendor/bundle/jruby/1.9/gems/logstash-core-2.2.4-java/lib/logstash/runner.rb:90  
call at org/jruby/RubyProc.java:281  
run at /opt/logstash/vendor/bundle/jruby/1.9/gems/logstash-core-2.2.4-java/lib/logstash/runner.rb:95  
call at org/jruby/RubyProc.java:281  
initialize at /opt/logstash/vendor/bundle/jruby/1.9/gems/stud-0.0.22/lib/stud/task.rb:24

**This error does not occur if I use the following code:**

ruby {  
code =\> ' allscores = event["score"]

```
                 t = allscores.inject(:+)
                 event["total_score"] = t
            '
   add_tag => [sum_score]
  }

```

**But I get an error**

**_Ruby exception occurred: undefined method `inject' for 5:Fixnum {:level=\>:error}_**

How can these error be mitigated?

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [January 2, 2017, 6:51am UTC](https://discuss.elastic.co/t/unexpected-error-in-ruby-code-in-logstash/70264/2 "2017-01-02T06:51:37Z")

</div>

If you use single quotes to delimit the Ruby code snippet you can't use single quotes inside the Ruby code. Your second attempt solves that but doesn't work because the `score` field contains an integer and `inject` doesn't work on integers. What are you trying to do here?

---

<div class="post-metadata">

**Author:** ![ANU\_SARA\_VARGHESE](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anu_sara_varghese/32/51487_2.png) [@ANU\_SARA\_VARGHESE](https://discuss.elastic.co/u/ANU_SARA_VARGHESE)\
**Post date:** [January 2, 2017, 7:12am UTC](https://discuss.elastic.co/t/unexpected-error-in-ruby-code-in-logstash/70264/3 "2017-01-02T07:12:16Z")

</div>

Hi Magnus,

I am trying to add the elements of the array **'score**'. It contains integers and it's length varies dynamically. This error occurs when the length of array is 1 i.e. the **score** contains only one value. If the length is more than one, the **inject function** works well and I get the **'total\_score'** and no error occurs.

If **inject** doesn't work for integer then I should have got error for **'total\_score**' .

What could be the reason for this?

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [January 2, 2017, 8:13am UTC](https://discuss.elastic.co/t/unexpected-error-in-ruby-code-in-logstash/70264/4 "2017-01-02T08:13:19Z")

</div>

> This error occurs when the length of array is 1 i.e. the score contains only one value.

No, it occurs when the value isn't an array at all.

```plaintext
$ irb
irb(main):001:0> [5].inject(:+)
=> 5
irb(main):002:0> 5.inject(:+)
NoMethodError: undefined method `inject' for 5:Fixnum
	from (irb):2
	from /usr/bin/irb:12:in `<main>'

```

If you need to support non-array `score` values just use a conditional or a ternary expression:

```plaintext
scores = event["score"]
event["total_score"] = scores.is_a?(Array) ? scores.inject(:+) : scores

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [January 30, 2017, 8:13am UTC](https://discuss.elastic.co/t/unexpected-error-in-ruby-code-in-logstash/70264/5 "2017-01-30T08:13:25Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
