# Undefined local variable

**URL:** <https://rubytalk.org/t/undefined-local-variable/65883>\
**Category:** ruby-talk\
**Created:** [12 July 2012 08:17 UTC](https://rubytalk.org/t/undefined-local-variable/65883 "2012-07-12T08:17:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [12 July 2012 08:17 UTC](https://rubytalk.org/t/undefined-local-variable/65883/1 "2012-07-12T08:17:14Z")

</div>

I use watir in Radrails for testing my app. Iuse 2 classes in my test,  
where is used variable @ie. Here are their code  
First class:

&nbsp;&nbsp;&nbsp;&nbsp;class Title  
&nbsp;&nbsp;&nbsp;&nbsp;def initialize(title,url,ie)  
&nbsp;&nbsp;&nbsp;&nbsp;@title = title  
&nbsp;&nbsp;&nbsp;&nbsp;@url=url  
&nbsp;&nbsp;&nbsp;&nbsp;@ie=ie  
&nbsp;&nbsp;&nbsp;&nbsp;@ie=Watir::IE.attach(:title, @title)  
&nbsp;&nbsp;&nbsp;&nbsp;rescue Watir::Exception::NoMatchingWindowFoundException  
&nbsp;&nbsp;&nbsp;&nbsp;puts ("could not find browser")  
&nbsp;&nbsp;&nbsp;&nbsp;$r.addtoReport(testReport, "check page element", "FAILED", "Page  
title " + @title +" not found")  
&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;@ie=Watir::IE.attach(:url, @url)  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end

Second class:

&nbsp;&nbsp;&nbsp;&nbsp;class Text\_pos  
&nbsp;&nbsp;&nbsp;&nbsp;def initialize(text, object,ie)  
&nbsp;&nbsp;&nbsp;&nbsp;@text=text  
&nbsp;&nbsp;&nbsp;&nbsp;@object=object  
&nbsp;&nbsp;&nbsp;&nbsp;@ie=ie  
&nbsp;&nbsp;&nbsp;&nbsp;if @ie.contains\_text(@text)  
&nbsp;&nbsp;&nbsp;&nbsp;puts("Test for " + @object + " passed")  
&nbsp;&nbsp;&nbsp;&nbsp;$r.addtoReport($testReport, "check " + @object, "PASSED", "Test for  
" + @object + " passed" )  
&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;puts("Test for " + @object + " failed")  
&nbsp;&nbsp;&nbsp;&nbsp;puts (@ie.link(:text =\> /Exception:/))  
&nbsp;&nbsp;&nbsp;&nbsp;h= @ie.link(:text =\> /Exception:/)  
&nbsp;&nbsp;&nbsp;&nbsp;$r.addtoReport($testReport, "check " + @object, "FAILED", h.text)  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
After in test body i executed such commands:

&nbsp;&nbsp;&nbsp;&nbsp;Text\_pos.new("Glossary Of Terms", "login",ie)  
&nbsp;&nbsp;&nbsp;&nbsp;Title.new("Company","[http://ec2-50-16-62-110.compute-1.amazonaws.com:9100/bobsworld/BPM/Mtebpm000p0001Form.do?resetFilter\_action=](http://ec2-50-16-62-110.compute-1.amazonaws.com:9100/bobsworld/BPM/Mtebpm000p0001Form.do?resetFilter_action=)",  
ie1)  
&nbsp;&nbsp;&nbsp;&nbsp;Text\_pos.new("Title", "if page is loaded", ie1)  
But get error - `Test for login passed  
BPM/try.rb:106:in `\<main\>': undefined local variable or method `ie1' for  
main:Object (NameError)`  
If i do not use class Title this Text\_pos.new("Title", "if page is  
loaded", ie1) works ok.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [12 July 2012 09:14 UTC](https://rubytalk.org/t/undefined-local-variable/65883/2 "2012-07-12T09:14:14Z")

</div>

i should first learn how ruby works before you use rails

you never defined your "ie1" variable in your code!

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [12 July 2012 09:36 UTC](https://rubytalk.org/t/undefined-local-variable/65883/3 "2012-07-12T09:36:29Z")

</div>

Also the sequence

&nbsp;&nbsp;&nbsp;&nbsp;@ie=ie  
&nbsp;&nbsp;&nbsp;&nbsp;@ie=Watir::IE.attach(:title, @title)

in class Title is dysfunctional: Argument ie is completely superfluous  
and can be removed since it is only written to @ie and @ie is  
overwritten immediately afterwards.

Kind regards

robert

> **···**
>
> On Thu, Jul 12, 2012 at 11:14 AM, Hans Mackowiak \<lists@ruby-forum.com\> wrote:
> 
> > i should first learn how ruby works before you use rails
> > 
> > you never defined your "ie1" variable in your code!
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)
