- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
所以我知道你可以说 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_get
、instance_variable_get
和const_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.u
、User::Validations.e
和 User::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/
标准ruby库中有const_defined?、const_get、const_set等一系列方法。 const_defined?, 常量获取, 常量集 而且,在 Rails 的 Active Sup
所以我知道你可以说 Kernel.const_get("ClassName") 并且你会得到字符串在名称中对应的类。但是对于变量呢?有没有办法: test = "heyas" some_method_
给定以下示例: [1] pry(main)> [2] pry(main)> module Foo [2] pry(main)* Foo = 'LOL' [2] pry(main)* ['Lol
我正在阅读 another question用 answer其中提到使用 Module#const_get 实例方法在模块中查找类。例如: module M class C end end p
根据文档 mod.const_get(sym) “返回 mod 中命名常量的值。” 我也知道 const_get 默认情况下可能会查找接收者的继承链。所以以下工作: class A; HELLO =
我最近发现 Ruby (2.2.1) 有一些“有趣”的行为。 module Foo class Foo end class Bar end end Foo.const_get('Foo
我有以下代码来提取和实例化 Rails Controller : def get_controller(route) name = route.requirements[:controller]
我错误地运行了以下脚本: rails g devise views 然后每当我尝试运行任何 rails 命令时都会收到此错误: `const_get': uninitialized constant
我在带有 Spring 和 Guard 的 Docker 中使用 Rails。请参阅保护文件: guard 'rspec', cmd: "#{env_vars} spring rspec -color
给定一个 User 类: class User end 我想使用 .class_eval 定义一个新常量.所以: User.class_eval { AVOCADO = 'fruit' } 如果我尝试
我已经被这个错误困扰了大约 3 天,我不知道如何更正它。任何帮助,将不胜感激。 顺便说一句,我在 Windows 机器上使用 Ruby v.1.9.3p392 & Rails v. 3.2.13 我在
我是一名优秀的程序员,十分优秀!