How to get the name of the arguments passed to the block?

In Ruby, you can do this:

prc = lambda{|x, y=42, *other|}
prc.parameters  #=> [[:req, :x], [:opt, :y], [:rest, :other]]

In particular, I am interested to know the names of the parameters xand yin the example above.

In Crystal, I have the following situation:

def my_method(&block)
  # I would like the name of the arguments of the block here
end

How to do it in Crystal?

+4
source share
2 answers

Ruby, Crystal , . , . . . , :

macro foo(&block)
  {{ block.args.first.stringify }}
end

p foo {|x| 0 } # => "x"
+6

Jonne Haß, Ruby parameters :

macro block_args(&block)
  {{ block.args.map &.symbolize }}
end
p block_args {|x, y, *other| } # => [:x, :y, :other]

, Crystal .

+5

Source: https://habr.com/ru/post/1694883/


All Articles