# JRuby problem

**URL:** <https://www.ruby-forum.com/t/jruby-problem/166197>\
**Category:** Ruby\
**Created:** [April 22, 2009, 5:45pm UTC](https://www.ruby-forum.com/t/jruby-problem/166197 "2009-04-22T17:45:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![amabdelsalam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/958977/32.png) [@amabdelsalam](https://www.ruby-forum.com/u/amabdelsalam)\
**Post date:** [April 22, 2009, 5:45pm UTC](https://www.ruby-forum.com/t/jruby-problem/166197/1 "2009-04-22T17:45:17Z")

</div>

Hi,…

I’m trying to execute the following lines:

ScriptContext sct = jruby.getContext();  
jruby.eval(“sheet = Spreadsheet.new”, sct);  
jruby.eval(“sheet.cells.a1 = ‘Welcome’”, sct);

The first two lines are executed correctly, but the last one gives the  
following error:  
undefined local variable or method `sheet’ for main:Object

it indicates that sheet is undefined although it’s defined in line 2.

When I try to execute both commands in a single statement like:  
jruby.eval(“sheet = Spreadsheet.new \n sheet.cells.a1 = ‘Welcome’”,  
sct);

The statement is executed correctly.

Please help

Regards,…

Ahmed

---

<div class="post-metadata">

**Author:** ![amabdelsalam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/958977/32.png) [@amabdelsalam](https://www.ruby-forum.com/u/amabdelsalam)\
**Post date:** [April 22, 2009, 6:09pm UTC](https://www.ruby-forum.com/t/jruby-problem/166197/2 "2009-04-22T18:09:59Z")

</div>

My guess is that the context fails to keep track of local variables. Try  
an  
experiment with something simpler:

ScriptContext sct = jruby.getContext  
jruby.eval(“name = ‘Ahmed’”)  
jruby.eval(‘puts “My name is #{name}”’)

If things explode, my theory is correct.

James

PS: Semi-colons aren’t required, nor are empty parenthesis. It’s  
considered  
out of the Ruby norm to use them. As such, I’d avoid them in your  
coding.  
See [http://www.pathf.com/blogs/ruby-and-rails-style-guide/](http://www.pathf.com/blogs/ruby-and-rails-style-guide/)

---

<div class="post-metadata">

**Author:** ![amabdelsalam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/958977/32.png) [@amabdelsalam](https://www.ruby-forum.com/u/amabdelsalam)\
**Post date:** [April 22, 2009, 6:15pm UTC](https://www.ruby-forum.com/t/jruby-problem/166197/3 "2009-04-22T18:15:58Z")

</div>

James H. wrote:

> My guess is that the context fails to keep track of local variables. Try  
> an  
> experiment with something simpler:
> 
> ScriptContext sct = jruby.getContext  
> jruby.eval(“name = ‘Ahmed’”)  
> jruby.eval(‘puts “My name is #{name}”’)
> 
> If things explode, my theory is correct.
> 
> James
> 
> PS: Semi-colons aren’t required, nor are empty parenthesis. It’s  
> considered  
> out of the Ruby norm to use them. As such, I’d avoid them in your  
> coding.  
> See [http://www.pathf.com/blogs/ruby-and-rails-style-guide/](http://www.pathf.com/blogs/ruby-and-rails-style-guide/)

Thanks for response. the code you sent is missing using sct, but I  
updated it:  
ScriptContext sct = jruby.getContext();  
jruby.eval(“name = ‘Ahmed’”, sct);  
jruby.eval(“puts "My name is #{name}"”, sct);

also not working, the same error: undefined local variable or method  
`name’ for main:Object

PS: Semi-colon is required by java code

---

<div class="post-metadata">

**Author:** ![amabdelsalam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/958977/32.png) [@amabdelsalam](https://www.ruby-forum.com/u/amabdelsalam)\
**Post date:** [April 22, 2009, 6:35pm UTC](https://www.ruby-forum.com/t/jruby-problem/166197/4 "2009-04-22T18:35:02Z")

</div>

Ahmed A. wrote:

> James H. wrote:
> 
> > My guess is that the context fails to keep track of local variables. Try  
> > an  
> > experiment with something simpler:
> > 
> > ScriptContext sct = jruby.getContext  
> > jruby.eval(“name = ‘Ahmed’”)  
> > jruby.eval(‘puts “My name is #{name}”’)
> > 
> > If things explode, my theory is correct.
> > 
> > James
> > 
> > PS: Semi-colons aren’t required, nor are empty parenthesis. It’s  
> > considered  
> > out of the Ruby norm to use them. As such, I’d avoid them in your  
> > coding.  
> > See [http://www.pathf.com/blogs/ruby-and-rails-style-guide/](http://www.pathf.com/blogs/ruby-and-rails-style-guide/)
> 
> Thanks for response. the code you sent is missing using sct, but I  
> updated it:  
> ScriptContext sct = jruby.getContext();  
> jruby.eval(“name = ‘Ahmed’”, sct);  
> jruby.eval(“puts "My name is #{name}"”, sct);
> 
> also not working, the same error: undefined local variable or method  
> `name’ for main:Object
> 
> PS: Semi-colon is required by java code

I found a solution for the problem, it’s a discussed bug:  
Take a look: [markmail.org/message/ze4vwkscc6w3cpc7](http://markmail.org/message/ze4vwkscc6w3cpc7)  
and [http://jira.codehaus.org/browse/JRUBY-371](http://jira.codehaus.org/browse/JRUBY-371)

A temporary solution is to add $ before variable name so that it’s seen  
in the same context with successive eval commands
