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

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.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---

"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

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

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

Regards,

Brian,

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

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

James Edward Gray II

···

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?

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/TestFirstUserInterfaces

catch/throw allow the creation of goto like behaviour, including
spaghetti code if abused.

Sam

Quoteing litleguy@bendbroadband.com, on Fri, Oct 15, 2004 at 08:04:28PM +0900:

···

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.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---

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

Hi everyone,
Thanks for the evil info.
I did not mean to force the GOTO taboo on Ruby. Just wanted to know.
Thanks again,
RK

···

On Mon, 18 Oct 2004 07:57:44 +0900, Sam Roberts <sroberts@uniserve.com> wrote:

catch/throw allow the creation of goto like behaviour, including
spaghetti code if abused.

Sam

Quoteing litleguy@bendbroadband.com, on Fri, Oct 15, 2004 at 08:04:28PM +0900:

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.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---

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

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. :slight_smile: Thanks for the enlightenment, Matt!

- Jamis

···

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

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.

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

10 x = 1
11 x += 1
12 puts x
15 goto 18 if x == 10
16 goto 11
18 puts "done"

Ewwwwww…lol... reminds me of walking into Radio Shack stores
as a kid and typing

10 PRINT "TRS-80 SUCKS"
20 GOTO 10

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

"dismember" ?

(Is woodchipper one word? :wink:

Regards,

Bill

···

From: "Matt Maycock" <ummaycoc@gmail.com>

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/~ummaycoc/nruby
(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
-------------

Dismember a dead thing? It will now follow you in parts!

Woodchipper doesn't count. You can't woodchipper a dead thing, same
as that you can't automobile it (you could woodchip it, or run over
it...)

Trust me - there isn't a word. But there should be!

~Me!

···

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

Dezombify.

···

On 2004-10-15, Bill Kelly <billk@cts.com> wrote:

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

"dismember" ?

You're right, there isn't ONE word. That's because everyone knows YOU SHOOT IT
IN THE HEAD!

(unless your talking about demonized dead things, but then there is little
hope anyway.)

:slight_smile:
T.

···

On Friday 15 October 2004 03:17 pm, Matt Maycock wrote:

Dismember a dead thing? It will now follow you in parts!

Woodchipper doesn't count. You can't woodchipper a dead thing, same
as that you can't automobile it (you could woodchip it, or run over
it...)

Trust me - there isn't a word. But there should be!

How about "hug"?

-- Markus

···

On Sat, 2004-10-16 at 06:49, Curt wrote:

On 2004-10-15, Bill Kelly <billk@cts.com> wrote:

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