Any equivalent of Ruby's public_send method?

后端 未结 2 1147
情话喂你
情话喂你 2021-01-23 19:02

In Ruby, to construct a method\'s name and send it to an object, one can do:

class Foo
  def foo
    \"FOO\"
  end
end

Foo.new.public_send(:foo)  # => \"FOO\         


        
2条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-23 19:36

    There is no equivalent in crystal. Crystal is a statically typed and statically compiled language. We have no send or eval functions.

    Depending on the problem you had which made you reach for send in the first place, you might be able to use macros. There is not one replacement for send in crystal, there is simply a bunch of tools to enable some dynamic behaviour to be modelled statically.

提交回复
热议问题