# Script not working

**URL:** <https://www.ruby-forum.com/t/script-not-working/136801>\
**Category:** Ruby\
**Created:** [May 6, 2008, 2:54pm UTC](https://www.ruby-forum.com/t/script-not-working/136801 "2008-05-06T14:54:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pranjal\_jain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ac91a4/32.png) [@pranjal\_jain](https://www.ruby-forum.com/u/pranjal_jain)\
**Post date:** [May 6, 2008, 2:54pm UTC](https://www.ruby-forum.com/t/script-not-working/136801/1 "2008-05-06T14:54:58Z")

</div>

HI All  
I am having a script which is not working when I am writing it inside a  
method  
def addcomment. But when I am writing it outside the method; it starts  
working. Can somebody explain me why this is so?

the script is as follows :

b = 0  
$ie = IE.new  
$ie.goto(“[https://tms.corp.xyz.com/](https://tms.corp.xyz.com/)”)  
$ie.bring\_to\_front  
$ie.maximize  
$ie.text\_field(:name, ‘{actionForm.username}’).set($username[b].to\_s)  
$ie.text\_field(:name, ‘{actionForm.password}’).set($password[b].to\_s)  
$ie.button(:name, ‘actionOverride:validateLogin.do’).click

if($ie.link(:class,“linkTableBody”).exists?)

begin  
$ie.link(:class,“linkTableBody”).click  
sleep 3  
cw = IE.attach( :title, /Timesheet - Add Comments/i )  
a = “Watir Automation using Ruby "  
cw.text\_field(:name,”{actionForm.resComm}").set(a)  
cw.button( “Save Comments” ).click

if a.length \>1024  
puts “Number of characters exceed the space specified”

#check\_for\_popups()

startClicker( "OK ", 7 , “User Input” )

#cw.text\_field(:name,“{actionForm.resComm}”).clear  
else  
puts “nthng”  
end  
#cw.link( “javascript:window.close()” ).click  
end  
else  
puts “Sorry”  
end

thanks inadvance 🙂

---

<div class="post-metadata">

**Author:** ![pranjal\_jain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ac91a4/32.png) [@pranjal\_jain](https://www.ruby-forum.com/u/pranjal_jain)\
**Post date:** [May 7, 2008, 6:38pm UTC](https://www.ruby-forum.com/t/script-not-working/136801/2 "2008-05-07T18:38:47Z")

</div>

On Tue, May 6, 2008 at 9:54 AM, Pranjal J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> HI All  
> I am having a script which is not working when I am writing it inside a  
> method  
> def addcomment. But when I am writing it outside the method; it starts  
> working. Can somebody explain me why this is so?

When you define a method, it’s not automatically executed by Ruby. So,  
if you want that code to run when the script is executed, you have to  
call the method in the main body of the script (or just put the code  
directly into the main body of the script, as you have done).

Example:

def add\_comment

# code goes here

end

if **FILE** == $0  
add\_comment # calls the method if we’re running the script.  
end
