# Goto in Ruby?

**URL:** <https://www.ruby-forum.com/t/goto-in-ruby/101903>\
**Category:** Ruby\
**Created:** [August 8, 2006, 4:51pm UTC](https://www.ruby-forum.com/t/goto-in-ruby/101903 "2006-08-08T16:51:02Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [August 8, 2006, 4:51pm UTC](https://www.ruby-forum.com/t/goto-in-ruby/101903/1 "2006-08-08T16:51:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [August 8, 2006, 5:12pm UTC](https://www.ruby-forum.com/t/goto-in-ruby/101903/2 "2006-08-08T17:12:06Z")

</div>

On 08.08.2006 16:47, aidy wrote:

> def enter\_data  
> $ie.text\_field(:name, ‘Address6’).set(line)
> 
> GB  
> 1  
> 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
```

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [August 8, 2006, 6:21pm UTC](https://www.ruby-forum.com/t/goto-in-ruby/101903/3 "2006-08-08T18:21:15Z")

</div>

Robert wrote

> ```
> $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

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [August 8, 2006, 6:33pm UTC](https://www.ruby-forum.com/t/goto-in-ruby/101903/4 "2006-08-08T18:33:17Z")

</div>

On 08.08.2006 18:18, aidy wrote:

> > ```
> > $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
```

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [August 9, 2006, 1:52pm UTC](https://www.ruby-forum.com/t/goto-in-ruby/101903/5 "2006-08-09T13:52:33Z")

</div>

Robert K. 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

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [August 9, 2006, 2:52pm UTC](https://www.ruby-forum.com/t/goto-in-ruby/101903/6 "2006-08-09T14:52:40Z")

</div>

On 09.08.2006 13:47, aidy wrote:

> Atherstone  
> Country:  
> Address 3 \_\_\_\_\_\_\_\_\_\_\_  
> this is the code  
> line = x.chomp  
> $ie.text\_field(:name, name).set(line)
> 
> 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
```

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [August 9, 2006, 5:01pm UTC](https://www.ruby-forum.com/t/goto-in-ruby/101903/7 "2006-08-09T17:01:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [August 9, 2006, 2:40pm UTC](https://www.ruby-forum.com/t/goto-in-ruby/101903/8 "2006-08-09T14:40:39Z")

</div>

On 8/9/06, aidy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Address:  
> Aidy Street  
> Address 1 \_\_\_\_\_\_\_\_\_\_\_  
> There are also two other fields: Country and Serach Results  
> y = 11  
> break if line.upcase ==‘EXPECTED-RESULT:’  
> end  
> $ie.text\_field(:name, name).set(line)  
> Aidy

This may not be the nicest solution, but it is flexible and it might get  
help you come up with a better solution. You can have any fields with  
any  
number of values.

Below is the code. If you copy and paste it into a ruby file it should  
run  
with some output.

Cheers

@test\_data = \<\<ETD  
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* 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  
ETD

def enter\_data  
results = Array.new  
current\_key = nil  
test\_index = 0

read\_in\_test\_data.each do |line|  
line.chomp!  
# skip to the next line and inc the test\_index if there is a line of  
stars  
if line =~ /^\*+/  
test\_index += 1  
results[test\_index] = Hash.new { Array.new }  
next  
end

```
# If it's in a test, check to see if it's a key
if line =~ /(.*)\:$/
  current_key = $1
  results[test_index][current_key] = Array.new
  next
end
results[test_index][current_key].push line

```

end

# Returns an array of hashes. Each hash value contains an array of

values  
results.compact  
end

def read\_in\_test\_data  
@test\_data.split(“\n”)  
end

enter\_data.each\_with\_index do |result, index|  
puts “Data for test #{index}”  
result.keys.each do |key|  
puts “Key: #{key}”  
result[key].each { |val| puts “Value: = #{val}” }  
end  
end

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [August 9, 2006, 7:47pm UTC](https://www.ruby-forum.com/t/goto-in-ruby/101903/9 "2006-08-09T19:47:15Z")

</div>

“aidy” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> address,  
> other times 5,6 or 7

Why don’t you use a ?
