Script not working

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/”)
$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 :slight_smile:

On Tue, May 6, 2008 at 9:54 AM, Pranjal J. [email protected]
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