[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Can one simulate go to in Ruby? Is it possible?

Roman K9

10/15/2004 11:00:00 AM

Hi,
Is it possible to have a go to in Ruby without having a go to?
If yes, How?
Thanks again, You guys are the best. Maybe Eiffel will have a similar
support one day.
RK


----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.new... The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---
17 Answers

Robert Klemme

10/15/2004 11:13:00 AM

0


"Roman K9" <litleguy@bendbroadband.com> schrieb im Newsbeitrag
news:uabvm0lrutuf59rls2vp7dvmscjk1e5rs5@4ax.com...
> Hi,
> Is it possible to have a go to in Ruby without having a go to?
> If yes, How?
> Thanks again, You guys are the best. Maybe Eiffel will have a similar
> support one day.
> RK

You might be able to do it with continuations (callcc) but I wonder what
you're cooking up there. Is this some kind of competition (putting "goto"
into languages that don't support it) or do you have a specific problem
that you think cannot be solved without "goto"?

Kind regards

robert

gabriele renzi

10/15/2004 12:11:00 PM

0

Roman K9 ha scritto:
> Hi,
> Is it possible to have a go to in Ruby without having a go to?
> If yes, How?
> Thanks again, You guys are the best. Maybe Eiffel will have a similar
> support one day.
> RK
>
>

I guess my previous mail did'nt make it to the ml/ng:

def goto(cont)
cont.call(cont)
end

label= callcc{|k| k }

puts 'loop'

goto label

exit! #never arrive here

Brian Candler

10/15/2004 12:13:00 PM

0

See if this is any help:
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-...

although it's more an academic exercise in understanding continuations
rather than something you'd actually want to use in practice.

Regards,

Brian,


James Gray

10/15/2004 12:54:00 PM

0

On Oct 15, 2004, at 6:04 AM, Roman K9 wrote:

> Hi,
> Is it possible to have a go to in Ruby without having a go to?
> If yes, How?

Everybody else has shown the black magic, so I'll take an easier
approach. Are you aware of catch/throw?

James Edward Gray II



Phlip

10/15/2004 1:00:00 PM

0

Roman K9 wrote:

> Is it possible to have a go to in Ruby without having a go to?

Thanks! Thanks a lot!

I was so careful to make sure I never even learned if Ruby _had_ a 'goto'
keyword, and now you go and post this!

--
Phlip
http://industrialxp.org/community/bin/view/Main/TestFirstUser...


Matt Maycock

10/15/2004 2:37:00 PM

0

--- code ---
#!/home/ummaycoc/bin/ruby -w

# Number stuff down the side
# REM means remark (NO number)
# No number means continue last number
# \ is same as no number...
# -d debug mode.

class NRuby
def initialize(debug = false)
@line = 0
@next = nil
@debug = debug
end

def goto(line)
@next = line
end

def stop()
@next = nil
end

def NRuby.error(msg)
$stderr.puts("NRubyErr: #{msg}")
exit(-1)
end

def debug(code, line)
if @debug then
puts "NO CODE #{line}" unless code[line]
puts "**#{line}**\n#{code[line]}\n" if code[line]
end
end


def NRuby.read(input, start)
code, prev = Hash.new(false), start
input.each {|line|
doPrev = false
case line
when /^\s*REM(\s+|(\s*$))/i then ;
when /^\s*$/ then ;
when /^\s*(\d+)\s+(.*)$/ then prev = $1.to_i
code[prev] = [$2]
when /^\s*\\\s+(.*)$/ then doPrev, line = true, $1
else doPrev = true
end
error "No Previous Line!" if prev.nil? && doPrev
code[prev] = [] unless code.include? prev
code[prev] << line if doPrev
}

code.each_key {|key|
code[key] = code[key].join(" \n")
}

code
end

def execute(code)
lastline = code.keys.max
until @line.nil? || @line > lastline
debug(code, @line)
@next = nil
eval(code[@line]) if code[@line]
@line = @next || @line + 1
end
end
end


debug = false
if ARGV.length > 0 then
if ARGV.include? "-d" then
debug = true
ARGV.delete "-d"
end
end

@___nruby___, start = NRuby.new(debug), nil

if ARGV.length > 0 then
stdin_read, lines = false, nil
until ARGV.empty? do
arg = ARGV.shift
case arg
when /^-+h/i
$stderr.puts <<-END_HELP
#{File.basename($0)} [-z|-s num] [-d] [files+]
-z sets the first number label to zero - for unlabeled code at
beginning of program.
-s num sets the first number label to num. Same use as -z.
-d debug mode.
files list what files to read from. - is stdin.

If no inputs are given, a program is read from stdin and executed.
END_HELP
exit(-1)
when '-'
NRuby.error "Already read from stdin." if stdin_read
stdin_read = true
lines = $stdin.readlines
when '-z'
start = 0
when '-s'
NRuby.error "No argument given with -s." if ARGV.empty?
start = ARGV.shift
NRuby.error "Argument to -s must be integer." unless start =~ /-?\d+$/
start = start.to_i
else
NRuby.error "#{arg} doesn't exist..." unless FileTest.exists?(arg)
NRuby.error "#{arg} isn't a file..." unless FileTest.file?(arg)
NRuby.error "#{arg} isn't readable..." unless FileTest.readable?(arg)
lines = File.readlines(arg)
end

lines ||= $stdin.readlines
lines.each {|l| l.chomp!}
@___nruby___.execute(NRuby.read(lines, start))
end
else
lines = $stdin.readlines.map {|l| l.chomp}
if lines.nil? || lines.empty? then
NRuby.error "No input given."
else
@___nruby___.execute(NRuby.read(lines, start))
end
end


--- a usage ---
[ummaycoc@queen ummaycoc]$ echo '
10 x = 1
11 x += 1
12 puts x
15 goto 18 if x == 10
16 goto 11
18 puts "done"
stop' | nruby
2
3
4
5
6
7
8
9
10
done
[ummaycoc@queen ummaycoc]$

--- usage 2 & 3 ---

[ummaycoc@queen bin]$ echo 'puts "hi"' | nruby
NRubyErr: No Previous Line!
[ummaycoc@queen bin]$ echo 'puts "hi"' | nruby -z
hi
[ummaycoc@queen bin]$

-------------------------------

--
There's no word in the English language for what you do to a dead
thing to make it stop chasing you.


Jamis Buck

10/15/2004 3:06:00 PM

0

Matt Maycock wrote:
[snip]
>
> --- a usage ---
> [ummaycoc@queen ummaycoc]$ echo '
> 10 x = 1
> 11 x += 1
> 12 puts x
> 15 goto 18 if x == 10
> 16 goto 11
> 18 puts "done"
> stop' | nruby
> 2
> 3
> 4
> 5
> 6
> 7
> 8
> 9
> 10
> done

That's just...wrong. Wow. :) Thanks for the enlightenment, Matt!

- Jamis

--
Jamis Buck
jgb3@email.byu.edu
http://www.jamisbuck...


Brian Candler

10/15/2004 3:40:00 PM

0

> class NRuby
...
> def execute(code)
...
> eval(code[@line]) if code[@line]

The trouble with that is that you are *recompiling* each line as you run it.
I would expect that to make it very slow.

That's why my BASIC-like attempt at RubyTalk:78458 used continuations, but
in two passes: first read through all the lines generating continuations but
not executing the blocks; then set a flag which lets them execute, and then
start again at the top (no central loop). The two-pass approach is needed so
that forward gotos work.

Admittedly it doesn't enforce numeric ordering of lines; if you do

line(10) { x }
line(30) { y }
line(20) { z }

then they will be executed in the sequence x, y, z. [Although as I wrote it,
the line with the lowest number is used as the starting point for execution]

Regards,

Brian.


Booker C. Bense

10/15/2004 3:58:00 PM

0

-----BEGIN PGP SIGNED MESSAGE-----

In article <e86cebfb041015073631769f2f@mail.gmail.com>,
Matt Maycock <ummaycoc@gmail.com> wrote:

[ Unholy morphing of Ruby into Basic deleted]

_ Man, that's just evil....

_ Booker C. Bense



-----BEGIN PGP SIGNATURE-----
Version: 2.6.2

iQCVAwUBQW/zcWTWTAjn5N/lAQF32wQAmlT3dbtxV2ZHZNexIauNNGPnT5CTsg0v
UV28wASRvsMLGi2gAnxx8JhkLxgumiF9q5xat01UOSrAmay6EoCo+0rPEoxSOR2B
VEBx3pXtJrU3Q3BsQzNRY3hHbZ1h0pujFsut/wz/4YfaOYtnG4w5ZOLVodkOB8Sq
hJ3Zh2O4VtI=
=JxsH
-----END PGP SIGNATURE-----

Matt Maycock

10/15/2004 6:21:00 PM

0

Actually, I made the fatal mistake of leaving some of that code in the
middle of a fix (a few months ago). Here's the good stuff:
http://www.cs.drexel.edu/~umma...
(was changing how stop() works, and taking care of a case of the -z
and -s i arguments (beginning line empty...))

--
There's no word in the English language for what you do to a dead
thing to make it stop chasing you.

--- code ---
#!/usr/bin/env ruby

# Number stuff down the side
# REM means remark (NO number)
# No number means continue last number
# \ is same as no number...
# -d debug mode.

class NRuby
def initialize(debug = false)
@line = 0
@next = nil
@debug = debug
end

def goto(line)
@next = line
end

def stop()
@next = :stop
end

def NRuby.error(msg)
$stderr.puts("NRubyErr: #{msg}")
exit(-1)
end

def debug(code, line)
if @debug then
puts "NO CODE #{line}" unless code[line]
puts "**#{line}**\n#{code[line]}\n" if code[line]
end
end


def NRuby.read(input, start)
code, prev = Hash.new(false), start
input.each {|line|
doPrev = false
case line
when /^\s*REM(\s+|(\s*$))/i then ;
when /^\s*$/ then ;
when /^\s*(\d+)\s+(.*)$/ then prev = $1.to_i
code[prev] = [$2]
when /^\s*\\\s+(.*)$/ then doPrev, line = true, $1
else doPrev = true
end
error "No Previous Line!" if prev.nil? && doPrev

unless prev.nil? then
code[prev] = [] unless code.include? prev
code[prev] << line if doPrev
end
}

code.each_key {|key|
code[key] = code[key].join(" \n")
}

code
end

def execute(code)
lastline = code.keys.max
until @line == :stop || @line > lastline
debug(code, @line)
@next = nil
eval(code[@line]) if code[@line]
@line = @next || @line + 1
end
end
end


debug = false
if ARGV.length > 0 then
if ARGV.include? "-d" then
debug = true
ARGV.delete "-d"
end
end

@___nruby___, start = NRuby.new(debug), nil

if ARGV.length > 0 then
stdin_read, lines = false, nil
until ARGV.empty? do
arg = ARGV.shift
case arg
when /^-+h/i
$stderr.puts <<-END_HELP
#{File.basename($0)} [-z|-s num] [-d] [files+]
-z sets the first number label to zero - for unlabeled code at
beginning of program.
-s num sets the first number label to num. Same use as -z.
-d debug mode.
files list what files to read from. - is stdin.

If no inputs are given, a program is read from stdin and executed.
END_HELP
exit(-1)
when '-'
NRuby.error "Already read from stdin." if stdin_read
stdin_read = true
lines = $stdin.readlines
when '-z'
start = 0
when '-s'
NRuby.error "No argument given with -s." if ARGV.empty?
start = ARGV.shift
NRuby.error "Argument to -s must be integer." unless start =~ /-?\d+$/
start = start.to_i
else
NRuby.error "#{arg} doesn't exist..." unless FileTest.exists?(arg)
NRuby.error "#{arg} isn't a file..." unless FileTest.file?(arg)
NRuby.error "#{arg} isn't readable..." unless FileTest.readable?(arg)
lines = File.readlines(arg)
end

lines ||= $stdin.readlines
lines.each {|l| l.chomp!}
@___nruby___.execute(NRuby.read(lines, start))
end
else
lines = $stdin.readlines.map {|l| l.chomp}
if lines.nil? || lines.empty? then
NRuby.error "No input given."
else
@___nruby___.execute(NRuby.read(lines, start))
end
end
-------------