[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Goto in Ruby?

aidy

8/8/2006 2:48:00 PM

I have a simple web GUI that I would like to read a file and enter data
into

This is my class

class Form
def initialize
@filename = "search.txt"
end

def enter_data
read_in_test_data.each { |x|
line = x.chomp
#need to log testId
next if line.upcase.include? 'TESTID'
next if line.upcase == 'ADDRESS:'
$ie.text_field(:name, 'Address1').set(line)
$ie.text_field(:name, 'Address2').set(line)
$ie.text_field(:name, 'Address3').set(line)
$ie.text_field(:name, 'Address4').set(line)
$ie.text_field(:name, 'Address5').set(line)
$ie.text_field(:name, 'Address6').set(line)
$ie.text_field(:name, 'Address7').set(line)
}
end

def read_in_test_data
#check if file exists and give date of last change
File.readlines(@filename, "\n" )
end
private:read_in_test_data
end



this is an example of the file I am reading

*********************************************** TESTID_10
Address:
30 Choyce Close
Atherstone
Warwickshire
Country:
GB
Search-Results:
20
*********************************************** TESTID_20
Address:
Hamilton Chartered Surveyors
Aidy Street
Bath
Country:
GB
Search-Results:
1
*********************************************** TESTID_30


Sometimes the address can be upto 1..7 lines. What I am looking for is
something like a goto
statement, that when 'Country:' is found a different field can be
filled in e.g.($ie.text_field(:name, 'Ctry1').set(line).

Thanks for the help

Cheers

Aidy

7 Answers

Robert Klemme

8/8/2006 3:09:00 PM

0

On 08.08.2006 16:47, aidy wrote:
> I have a simple web GUI that I would like to read a file and enter data
> into
>
> This is my class
>
> class Form
> def initialize
> @filename = "search.txt"
> end
>
> def enter_data
> read_in_test_data.each { |x|
> line = x.chomp
> #need to log testId
> next if line.upcase.include? 'TESTID'
> next if line.upcase == 'ADDRESS:'
> $ie.text_field(:name, 'Address1').set(line)
> $ie.text_field(:name, 'Address2').set(line)
> $ie.text_field(:name, 'Address3').set(line)
> $ie.text_field(:name, 'Address4').set(line)
> $ie.text_field(:name, 'Address5').set(line)
> $ie.text_field(:name, 'Address6').set(line)
> $ie.text_field(:name, 'Address7').set(line)
> }
> end
>
> def read_in_test_data
> #check if file exists and give date of last change
> File.readlines(@filename, "\n" )
> end
> private:read_in_test_data
> end
>
>
>
> this is an example of the file I am reading
>
> *********************************************** TESTID_10
> Address:
> 30 Choyce Close
> Atherstone
> Warwickshire
> Country:
> GB
> Search-Results:
> 20
> *********************************************** TESTID_20
> Address:
> Hamilton Chartered Surveyors
> Aidy Street
> Bath
> Country:
> GB
> Search-Results:
> 1
> *********************************************** TESTID_30
>
>
> Sometimes the address can be upto 1..7 lines. What I am looking for is
> something like a goto
> statement, that when 'Country:' is found a different field can be
> filled in e.g.($ie.text_field(:name, 'Ctry1').set(line).
>
> Thanks for the help
>
> Cheers
>
> Aidy
>

Use a CASE statement for this.

case line.upcase
when 'ADDRESS:'
$ie.text_field(:name, 'Address1').set(line)
$ie.text_field(:name, 'Address2').set(line)
$ie.text_field(:name, 'Address3').set(line)
$ie.text_field(:name, 'Address4').set(line)
$ie.text_field(:name, 'Address5').set(line)
$ie.text_field(:name, 'Address6').set(line)
$ie.text_field(:name, 'Address7').set(line)
when 'TESTID'
# ...
else
# raise or what?
end

You can as well create more complex but flexible mechanisms (e.g. look
up a lambda in a Hash) but whether it's worth the effort depends on your
needs.

HTH

Kind regards

robert

aidy

8/8/2006 4:18:00 PM

0

Robert wrote

> Use a CASE statement for this.
>
> case line.upcase
> when 'ADDRESS:'
> $ie.text_field(:name, 'Address1').set(line)
> $ie.text_field(:name, 'Address2').set(line)
> $ie.text_field(:name, 'Address3').set(line)
> $ie.text_field(:name, 'Address4').set(line)
> $ie.text_field(:name, 'Address5').set(line)
> $ie.text_field(:name, 'Address6').set(line)
> $ie.text_field(:name, 'Address7').set(line)

Thanks for the advice, but this will only enter the word 'address' in 7
different boxes, while I need to iterate through the arrray.

Cheers

aidy

Robert Klemme

8/8/2006 4:27:00 PM

0

On 08.08.2006 18:18, aidy wrote:
> Robert wrote
>
>> Use a CASE statement for this.
>>
>> case line.upcase
>> when 'ADDRESS:'
>> $ie.text_field(:name, 'Address1').set(line)
>> $ie.text_field(:name, 'Address2').set(line)
>> $ie.text_field(:name, 'Address3').set(line)
>> $ie.text_field(:name, 'Address4').set(line)
>> $ie.text_field(:name, 'Address5').set(line)
>> $ie.text_field(:name, 'Address6').set(line)
>> $ie.text_field(:name, 'Address7').set(line)
>
> Thanks for the advice, but this will only enter the word 'address' in 7
> different boxes, while I need to iterate through the arrray.

You probably should state more clearly what you need. Then we can come
up with better suggestions.

robert

aidy

8/9/2006 11:48:00 AM

0


Robert Klemme wrote:

> You probably should state more clearly what you need. Then we can come
> up with better suggestions.

This is an example of a file I am reading.

********************************************** TESTID_10
Address:
30 Choyce Close
Atherstone
Country:
GB
Search-Results:
20
EXPECTED-RESULT:
*********************************************** TESTID_20
Address:
Hamilton Chartered Surveyors
Aidy Street
Bath
Country:
GB
Search-Results:
1
EXPECTED-RESULT:
*********************************************** TESTID_30

The GUI looks something like this

Address 1 ___________
Address 2 ___________
Address 3 ___________
Address 4 ___________

etc upto 7

However in some cases I will have one or two lines to enter in the
address,
other times 5,6 or 7

There are also two other fields: Country and Serach Results

this is the code

class Form

def initialize
@filename = "search.txt"
end

def enter_data
y = 11
read_in_test_data.each { |x|
line = x.chomp
p line
if line.upcase != 'ADDRESS:' and !line.upcase.include? 'TESTID'
name = 'A' + y.to_s

next if line.upcase == 'COUNTRY:'
$ie.text_field(:name, 'C1').set(line)
next if line.upcase =='SEARCH-RESULTS:'
$ie.text_field(:name, 'NoResults').set(line)
break if line.upcase =='EXPECTED-RESULT:'

$ie.text_field(:name, name).set(line)
y = y + 1
end
}
end

def read_in_test_data
#check if file exists and give date of last change
File.readlines(@filename, "\n" )
end
private:read_in_test_data

end

My 'logic' is when the file is read if we do not see ADDRESS: or TESTID
then we must be at the point to enter the address

y = 11
if line.upcase != 'ADDRESS:' and !line.upcase.include? 'TESTID'
name = 'A' + y.to_s
$ie.text_field(:name, name).set(line)
y = y + 1

the object names for addresses start from A11, hence the iterator.

However if I hit 'COUNTRY:', I need to put the country in the country
field (hence the next if [which in both cases will be GB), then
the search result in the SEARCH-RESULTS: field.

Cheers

Aidy

Robert Klemme

8/9/2006 12:45:00 PM

0

On 09.08.2006 13:47, aidy wrote:
> Robert Klemme wrote:
>
>> You probably should state more clearly what you need. Then we can come
>> up with better suggestions.
>
> This is an example of a file I am reading.
>
> ********************************************** TESTID_10
> Address:
> 30 Choyce Close
> Atherstone
> Country:
> GB
> Search-Results:
> 20
> EXPECTED-RESULT:
> *********************************************** TESTID_20
> Address:
> Hamilton Chartered Surveyors
> Aidy Street
> Bath
> Country:
> GB
> Search-Results:
> 1
> EXPECTED-RESULT:
> *********************************************** TESTID_30
>
> The GUI looks something like this
>
> Address 1 ___________
> Address 2 ___________
> Address 3 ___________
> Address 4 ___________
>
> etc upto 7
>
> However in some cases I will have one or two lines to enter in the
> address,
> other times 5,6 or 7
>
> There are also two other fields: Country and Serach Results
>
> this is the code
>
> class Form
>
> def initialize
> @filename = "search.txt"
> end
>
> def enter_data
> y = 11
> read_in_test_data.each { |x|
> line = x.chomp
> p line
> if line.upcase != 'ADDRESS:' and !line.upcase.include? 'TESTID'
> name = 'A' + y.to_s
>
> next if line.upcase == 'COUNTRY:'
> $ie.text_field(:name, 'C1').set(line)
> next if line.upcase =='SEARCH-RESULTS:'
> $ie.text_field(:name, 'NoResults').set(line)
> break if line.upcase =='EXPECTED-RESULT:'
>
> $ie.text_field(:name, name).set(line)
> y = y + 1
> end
> }
> end
>
> def read_in_test_data
> #check if file exists and give date of last change
> File.readlines(@filename, "\n" )
> end
> private:read_in_test_data
>
> end
>
> My 'logic' is when the file is read if we do not see ADDRESS: or TESTID
> then we must be at the point to enter the address
>
> y = 11
> if line.upcase != 'ADDRESS:' and !line.upcase.include? 'TESTID'
> name = 'A' + y.to_s
> $ie.text_field(:name, name).set(line)
> y = y + 1
>
> the object names for addresses start from A11, hence the iterator.
>
> However if I hit 'COUNTRY:', I need to put the country in the country
> field (hence the next if [which in both cases will be GB), then
> the search result in the SEARCH-RESULTS: field.
>
> Cheers
>
> Aidy
>

task = nil
count = 0

case line
when /^\*+ TESTID_\d+$/
# ignore
when /^Address:$/
task = :address
when /^Country:$/
task = :country
....
else
case task
when :address
$ie.text_field(:name, "A#{count}").set(line)
count += 1
when :country
$ie.text_field(:name, "C#{count}").set(line)
count += 1
....
else
# ignore or raise exception
end
end

robert

aidy

8/9/2006 2:59:00 PM

0

Hi Robert

took your advice, and works like a dream. I owe you a couple of beers.

def enter_data
task = nil
count = 11

read_in_test_data.each { |x|
line = x.chomp

case line
when /^\*+ TESTID_\d+$/
# log
when /^Address:$/
task = :address
when /^Country:$/
task = :country
when /^Search-Results:$/
task = :search
else
case task
when :address
$ie.text_field(:name, "A#{count}").set(line)
count += 1
when :country
$ie.text_field(:name, 'C1').set(line)
when :search
$ie.text_field(:name, 'NoResults').set(line)
$ie.button(:value, 'Search').click
#need to now verify
else
#whatever
end
end
}

end

aidy

Christian Neukirchen

8/9/2006 5:45:00 PM

0

"aidy" <aidy.rutter@gmail.com> writes:

> The GUI looks something like this
>
> Address 1 ___________
> Address 2 ___________
> Address 3 ___________
> Address 4 ___________
>
> etc upto 7
>
> However in some cases I will have one or two lines to enter in the
> address,
> other times 5,6 or 7

Why don't you use a <textarea>?

> Aidy
--
Christian Neukirchen <chneukirchen@gmail.com> http://chneuk...