gpt4 book ai didi

Ruby:获取变量名

转载 作者:数据小太阳 更新时间:2023-10-29 06:53:40 30 4
gpt4 key购买 nike

<分区>

如何获取变量名?例如,

def get_var_name(var)
# return variable name
end

myname = nil
get_var_name myname #=> myname

初始目的:

somevar = "value"

puti somevar #=> somevar = "value"
# that is a shortage for
# `puts "somevar = #{somevar.inspect}"`

我的尝试:

def puti(symb)
var_name = symb.to_s
var_value = eval(var_name)
puts "#{var_name} = #{var_value.inspect}"
end
puti :@somevar # that actually will work only with class vars or whatever considering var scope;

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com