Trying to form a Goto method

0 def goto(label)
1 calls = File.open(FILE).read
2 if calls.lines.to_s.include?(label) == true
3 label_checker = {}
4 calls.lines.each {|x|
label_checker.store(calls.lines.find_index(x),
5 x.to_s.include?(label))} == true
6 i = 0
7 while i <= label_checker.length do
8 if label_checker[i] == true
9 puts calls.lines.to_a[i]
10 puts label
11 puts calls.lines.to_a[i].equal?(label)
12 if calls.lines.to_a[i].equal?(label) == true
13 puts “come on now…” #calls.lines.to_a[i]
14 end
15 end
16 i += 1
17 end
18 else
19 return “#{label} not initialized”
20 end
21 end
22
23 goto(“#label001:”)
24 #label002:
25 puts “hmm… here…”
26 exit
27 #label001:
28 puts “but not here…”
29 puts file = File.open(FILE).read
30 puts file.lines.index
31 goto(“#label002:”)


This is what i have so far and im using
http://www.compileonline.com/execute_ruby_online.php as the interpreter.
What i don’t understand is line 11 should equal true… yet returns
false.
Any thoughts, and I know about call.cc and catch / throw this is mainly
something to code to the way i enjoy.

“lines” will give you all the lines with a carriage return on the end,
and you’re comparing to a string which does not have a carriage-return,
so they will never match.

def goto label
File.open(FILE).readlines.map(&:chomp).index(label)
end

or without chomping every line:

def goto label
a = File.open(FILE).readlines
(1…a.length).select { |i| a[i-1] =~ /\A#{ label }\s+/ }.first
end

Actually using “find” rather than “select” should return the first,
rather than continuing to scan through everything.

ah noted, thank you for the response Joel, I’ll see what i can work out
with your insight.

I got it working, again Joel Thank you very much.