Ruby中有后藤语句吗?

rta7y2nd  于 2023-06-22  发布在  Ruby
关注(0)|答案(5)|浏览(112)

有没有一种方法可以从指定的行开始,比如后藤语句?

wlzqhblo

wlzqhblo1#

首先,它将是一个声明,而不是一个命令。第二,参见ruby-goto。三、注意事项

**类别:**图书馆/邪恶

ecr0jaav

ecr0jaav2#

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是,嗯,一个笑话。我不相信下载链接曾经工作过。或者我应该指给别人看然后保持沉默?我不知道...
我喜欢Ryan在宣布ruby-goto之后的下一句话:
敬请期待下一个邪恶模块。。ruby-malloc!祝你今天愉快
瑞安显然是个天才。

lkaoscv7

lkaoscv73#

我不这么认为(而且,以神圣的名义,它不应该)。
但是如果你真的有受虐狂的感觉,有一个goto模块。

xriantvc

xriantvc4#

后藤库仍然与我们同在: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
shstlldc

shstlldc5#

我尝试创建一个gist来实现它的功能,this is how it went
我的图书馆是如何工作的:

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

它的输出是:

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

请注意,我的库不以任何方式、形状或形式使用异常。另外,请注意,Ruby团队没有在语言中实现goto是有原因的,我做的这个东西只是为了好玩:)

相关问题