我如何访问参数?

时间:2021-04-02 20:13:52

I want a method to be used within another method and returns the arguments without the need of mentioning the argument names, something like return_arguments in the following.

我想要一个方法在另一个方法中使用并返回参数而不需要提及参数名称,如下面的return_arguments。

def foo a, b, *c
  ... # part a
  p return_arguments
  ... # part b
end

foo(1, "blah blah", :a, :b)
... # return from part a
# => [1, "blah blah", :a, :b]
... # return from part b

Is this possible? I figured out that binding, local_variables, and eval may be used, but am not sure how to distinguish the arguments from other local variables defined in part a above. Is there a particular ordering rule for local_variables? If so, maybe I can use that together with arity to extract the arguments.

这可能吗?我发现可以使用binding,local_variables和eval,但我不确定如何将参数与上面部分a中定义的其他局部变量区分开来。是否有local_variables的特定排序规则?如果是这样,也许我可以和arity一起使用它来提取参数。

3 个解决方案

#1


1  

It's definitely inefficient, but here's a solution (although you probably should be using a hash like @tadman mentioned):

这绝对是低效的,但这是一个解决方案(虽然您可能应该使用像@tadman提到的哈希):

set_trace_func proc { |e,_,_,i,b,_|
   if e == 'call'
      p = method(i).parameters
      @arguments = p.map {|p| eval(p[1].to_s,b)}
   end
}

def foo a, b, *c
   a = 50
   b = 3
   return @arguments
   a = 11
   b = 2
end

p foo(1, "blah blah", :a, :b)

#2


1  

Use local_variables first thing in the method, i.e. before setting any local variables, and save the result for later use.

首先在方法中使用local_variables,即在设置任何局部变量之前,并保存结果供以后使用。

#3


1  

Probably the easiest thing to do would be something like this:

可能最容易做的事情是这样的:

def foo(*args)
  a, b, *c = *args
  # (...)
  p args
  # (...)
end

Or conversely:

或者相反:

def foo(a, b, *c)
  args = [a, b, *c]
  # (...)
  p args
  # (...)
end

I'm not sure why you are opposed to mentioning the argument names at least once. It's most straightforward and readable to either assign your "arguments" inside your method (letting to keep the full array) or to reconstruct the array from the parsed arguments.

我不确定你为什么反对至少提一次提到参数名称。在方法中分配你的“参数”(让保持完整的数组)或从解析的参数重建数组是最简单和可读的。

#1


1  

It's definitely inefficient, but here's a solution (although you probably should be using a hash like @tadman mentioned):

这绝对是低效的,但这是一个解决方案(虽然您可能应该使用像@tadman提到的哈希):

set_trace_func proc { |e,_,_,i,b,_|
   if e == 'call'
      p = method(i).parameters
      @arguments = p.map {|p| eval(p[1].to_s,b)}
   end
}

def foo a, b, *c
   a = 50
   b = 3
   return @arguments
   a = 11
   b = 2
end

p foo(1, "blah blah", :a, :b)

#2


1  

Use local_variables first thing in the method, i.e. before setting any local variables, and save the result for later use.

首先在方法中使用local_variables,即在设置任何局部变量之前,并保存结果供以后使用。

#3


1  

Probably the easiest thing to do would be something like this:

可能最容易做的事情是这样的:

def foo(*args)
  a, b, *c = *args
  # (...)
  p args
  # (...)
end

Or conversely:

或者相反:

def foo(a, b, *c)
  args = [a, b, *c]
  # (...)
  p args
  # (...)
end

I'm not sure why you are opposed to mentioning the argument names at least once. It's most straightforward and readable to either assign your "arguments" inside your method (letting to keep the full array) or to reconstruct the array from the parsed arguments.

我不确定你为什么反对至少提一次提到参数名称。在方法中分配你的“参数”(让保持完整的数组)或从解析的参数重建数组是最简单和可读的。