gpt4 book ai didi

ruby - const_get 但对于变量

转载 作者:太空宇宙 更新时间:2023-11-03 17:06:01 26 4
gpt4 key购买 nike

所以我知道你可以说 Kernel.const_get("ClassName") 并且你会得到字符串在名称中对应的类。但是对于变量呢?有没有办法:

test = "heyas"
some_method_here("test") #=> "heyas"

非常感谢

事实是我在更复杂的代码中需要它,真实的例子:

class User
class Validations
class << self
def username_len
return u.len > 3 and u.len < 22
end
# ...
end
end
def validate(u,e,p)
[:u, :e, :p].each do |v|
Validations.send(:define_singleton_method, v, lambda { eval(v.to_s) }) # see this line
end
#other code to run validations
end
end

所以你看,没有更好的方法,是吗?

最佳答案

没有,只有class_variable_getinstance_variable_getconst_get。没有 local_variable_get。但这无论如何都没有意义:局部变量是当前方法体、模块体、类体或脚本体的局部变量,这就是它们被称为“局部”变量的原因,毕竟!您只是无法从其他方法访问它们。

Is there a way to do:

test = "heyas"
some_method_here("test") #=> "heyas"

不,没有办法做到这一点。而且不可能有办法做到这一点。 test 是一个本地 变量,这意味着它 存在于当前脚本主体中。它存在于some_method_here 的正文中。这就是局部变量的重点:在任何情况下,您都无法从其他地方访问它们。

关于您对另一个答案的评论:

def my_func(str)
var_a = 'hey'
var_b = 'ah'
return some_method_here(str)
end
#=> should return the corresponding variable's value, e.g. my_func('var_a')
#=> 'hey'

同样,这不可能起作用,因为局部变量的重点是它们不能从其他任何地方访问。 p>

但是有一个非常简单的变体可以完全满足您的需求:

def my_func(str)
{
'var_a' => 'hey',
'var_b' => 'ah'
}[str]
end
#=> should return the corresponding variable's value, e.g. my_func('var_a')
#=> 'hey'

这当然可以进一步简化为:

my_func = {'var_a' => 'hey', 'var_b' => 'ah'}
#=> should return the corresponding variable's value, e.g. my_func['var_a']
#=> 'hey'

鉴于您只能传递有限数量的不同选项,最好改用符号:

my_func = {var_a: 'hey', var_b: 'ah'}
#=> should return the corresponding variable's value, e.g. my_func[:var_a]
#=> 'hey'

您基本上是在问:传入一个键,得到一个值。 正是Hash 是什么。

编辑:修改后的问题,这是我能想到的最好的:

def validate(u, e, p)
local_variables.zip(local_variables.map {|var|
eval(var.to_s)
}).each {|var, val|
Validations.send(:define_singleton_method, var) { val }
}
end

但是,我认为设计存在严重错误。您根据User 的不同实例 覆盖User::Validations单例方法。根据 singleton 方法的定义,系统中永远只能有一个副本。但是你有许多不同的 User 实例,每次调用 User#validate 时,它都会覆盖 User::Validations.uUser::Validations.eUser::Validations.p 的副本,此时他们将开始整个系统的行为完全不同。

换句话说,您正在根据单个实例 更改整个系统 的行为。并且可以有许多实例,并且每次,系统的行为都会发生变化。

不可能是对的。

u1 = User.new
u1.validate('u1', :e1, 1)

p User::Validations.u, User::Validations.e, User::Validations.p
# => 'u1'
# => :e1
# => 1

u2 = User.new
u2.validate('U2', :E2, 2.0)
# => warning: method redefined; discarding old u
# => warning: method redefined; discarding old e
# => warning: method redefined; discarding old p
# ^^^^^^^^^^^^^^^^
# Oops!

p User::Validations.u, User::Validations.e, User::Validations.p
# => 'U2'
# => :E2
# => 2.0
# ^^^
# Completely different results for the exact same arguments

关于ruby - const_get 但对于变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3019359/

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