18

Is there a way to start at a specified line, like a goto statement?

Shahriar
  • 13,460
  • 8
  • 78
  • 95
gpwu
  • 6,957
  • 3
  • 17
  • 9

5 Answers5

28

First, it would be statement, not a command. Second, see ruby-goto. Third, note

Category: Library/Evil

Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
  • 13
    Wow, "Category: Library/Evil". – LiraNuna Oct 28 '09 at 02:39
  • 3
    At first I thought this was a joke, but there actually Is a Category: Library/Evil. It currently includes ruby-goto as mentioned, as well as *segfault4r* : "Its a module that forces a segfault. – Paul Oct 28 '09 at 02:52
4

There is the ruby command line switch -x.

 -x[directory]  Tells Ruby that the script is embedded in a message.
                Leading garbage will be discarded until the first that
                starts with “#!” and contains the string, “ruby”.  Any
                meaningful switches on that line will applied.  The end of
                script must be specified with either EOF, ^D (control-D),
                ^Z (control-Z), or reserved word __END__.  If the direc‐
                tory name is specified, Ruby will switch to that directory
                before executing script.

BTW, I'm pretty sure ruby-goto was, umm, a joke. I don't believe the download link has ever worked. Or am I just supposed to point people to it and keep quiet? I never know...

I liked Ryan's next line after announcing ruby-goto:

Stay tuned for the next evil module... ruby-malloc! Have a nice day.

Ryan is clearly a genius.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
DigitalRoss
  • 143,651
  • 25
  • 248
  • 329
2

I don't believe so (and, by all that's holy, it shouldn't).

But there's a goto module for it if you're feeling really masochistic.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
2

The goto lib is still with us :D https://rubygems.org/gems/goto/versions/0

Conserving the entire gem for posterity:

STACK = []

class Label
  attr_accessor :name;
  attr_accessor :block;

  def initialize(name, block);
    @name  = name
    @block = block
  end

  def ==(sym)
    @name == sym
  end
end

class Goto < Exception;
  attr_accessor :label
  def initialize(label); @label = label; end
end

def label(sym, &block)
  STACK.last << Label.new(sym, block)
end

def frame_start
  STACK << []
end

def frame_end
  frame = STACK.pop
  idx   = 0

  begin
    for i in (idx...frame.size)
      frame[i].block.call if frame[i].block
    end
  rescue Goto => g
    idx = frame.index(g.label)
    retry
  end
end

def goto(label)
  raise Goto.new(label)
end
bbozo
  • 7,075
  • 3
  • 30
  • 56
0

I took a stab at creating a gist that implements its functionality, this is how it went.

Example of how my library works:

label_stack_main do
  x = 0

  label(:increment) do
    x += 1
    goto :example
  end

  label(:example) do
    puts x
    goto :increment if x < 10
  end
  
  label(:start) do
    goto :example
  end
end

And it's output:

$ ruby example.rb
1
2
3
4
5
6
7
8
9
10

Note that my library DOES NOT utilize exceptions in any way, shape or form. Also, note that there's a reason the Ruby team didn't implement goto natively into the language, this thing I've made was just for fun :)

Shannarra
  • 521
  • 4
  • 17