Ruby中有goto语句吗?

时间:2009-10-28 02:35:01

标签: ruby goto

有没有办法从指定的行开始,比如goto语句?

4 个答案:

答案 0 :(得分:28)

首先,它将是声明,而不是命令。其次,请参阅ruby-goto。第三,注意

  

类别:图书馆/邪恶

答案 1 :(得分:4)

有ruby命令行开关-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.
顺便说一下,我很确定ruby-goto是,嗯,是个笑话。我不相信下载链接曾经有效。或者我只是想让人们注意并保持安静?我不知道......

在宣布ruby-goto之后,我喜欢Ryan的下一行:

  

请继续关注下一个邪恶模块......   红宝石的malloc!祝你有愉快的一天。

瑞安显然是个天才。

答案 2 :(得分:2)

我不相信(并且,所有这些都是圣洁的,不应该)。

但如果你感觉真的很自虐,那就有一个goto模块。

答案 3 :(得分:1)

goto lib仍在我们身边:D https://rubygems.org/gems/goto/versions/0

为后代保存整个宝石:

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