Pointers 晶体方法指针

Pointers 晶体方法指针,pointers,methods,crystal-lang,Pointers,Methods,Crystal Lang,可以在Crystal中使用方法指针吗 如果是,我如何创建和使用它们 我希望将多条指令存储在一个数组中,然后稍后在程序中调用所需的指令。这就是Proc的作用 def foo() puts "foo" end def bar() puts "bar" end procs = {->foo, ->bar} procs.each do |p| p.call end 输出 foo bar 见在线: 它也适用于方法和静态方法: class A def self.foo(

可以在Crystal中使用方法指针吗

如果是,我如何创建和使用它们


我希望将多条指令存储在一个数组中,然后稍后在程序中调用所需的指令。

这就是
Proc
的作用

def foo()
  puts "foo"
end

def bar()
  puts "bar"
end

procs = {->foo, ->bar}

procs.each do |p|
  p.call
end
输出

foo
bar
见在线:

它也适用于方法和静态方法:

class A
  def self.foo()
    puts "A.foo"
  end

  def bar()
    puts "bar"
  end
end

proc = ->A.foo

puts proc
proc.call

a = A.new

proc = ->a.bar
proc.call


在官方文档中阅读更多信息:

这就是
Proc
的作用

def foo()
  puts "foo"
end

def bar()
  puts "bar"
end

procs = {->foo, ->bar}

procs.each do |p|
  p.call
end
输出

foo
bar
见在线:

它也适用于方法和静态方法:

class A
  def self.foo()
    puts "A.foo"
  end

  def bar()
    puts "bar"
  end
end

proc = ->A.foo

puts proc
proc.call

a = A.new

proc = ->a.bar
proc.call

在官方文件中阅读更多内容: