Rails:将字符串转换为变量(以存储值)

发布于 2024-12-06 13:20:26 字数 1254 浏览 1 评论 0原文

我有一个参数哈希,其中包含不同的变量和名称对,例如:

param_hash = {"system_used"=>"metric", "person_height_feet"=>"5"}

我还有一个对象 CalculationValidator,它不是 ActiveRecord,而是 ActiveModel::Validations。该对象验证来自表单的不同类型的输入。因此它没有一组特定的变量。

我想创建一个对象来验证它,如下所示:

validator = CalculationValidator.new()
validator.system_used = "metric"
validator.person_height_feet = 5

validator.valid?

我现在的问题是我真的不喜欢手动编写每个 CalculationValidator 代码,而是使用哈希中的信息。信息都在那里,所以我想做的是这样的,其中 MAKE_INTO_VARIABLE() 是我正在寻找的功能。

validator = CalculationValidator.new()
param_hash.each do |param_pair|
  ["validator.", param_pair[0]].join.MAKE_INTO_VARIABLE() = param_pair[1]
  # thus creating
  # "validator.system_used".MAKE_INTO_VARIABLE() = "metric"
  # while wanting: validator.system_used = "metric"
  # ...and in the next loop
  # "validator.person_height_feet".MAKE_INTO_VARIABLE() = 5
  # while wanting: validator.person_height_feet = 5
end

validator.valid?

问题: 基本上我的问题是,如何将字符串“validator.person_height”放入可用于存储数字 5 的变量 validator.person_height 中?

此外,将 param_pair[1] 的值存储为其真实格式(整数、字符串等)非常重要,因为它们将被验证。

我已经尝试过 .send() 和 instance_variable_set 但我不确定它们是否能解决问题。

I have a parameter hash that contains different variable and name pairs such as:

param_hash = {"system_used"=>"metric", "person_height_feet"=>"5"}

I also have an object CalculationValidator that is not an ActiveRecord but a ActiveModel::Validations. The Object validates different types of input from forms. Thus it does not have a specific set of variables.

I want to create an Object to validate it like this:

validator = CalculationValidator.new()
validator.system_used = "metric"
validator.person_height_feet = 5

validator.valid?

my problem right now is that I really would not prefer to code each CalculationValidator manually but rather use the information in the Hash. The information is all there so what I would like to do is something like this, where MAKE_INTO_VARIABLE() is the functionality I am looking for.

validator = CalculationValidator.new()
param_hash.each do |param_pair|
  ["validator.", param_pair[0]].join.MAKE_INTO_VARIABLE() = param_pair[1]
  # thus creating
  # "validator.system_used".MAKE_INTO_VARIABLE() = "metric"
  # while wanting: validator.system_used = "metric"
  # ...and in the next loop
  # "validator.person_height_feet".MAKE_INTO_VARIABLE() = 5
  # while wanting: validator.person_height_feet = 5
end

validator.valid?

Problem:
Basically my problem is, how do I make the string "validator.person_height" into the variable validator.person_height that I can use to store the number 5?

Additionally, it is very important that the values of param_pair[1] are stored as their real formats (integer, string etc) since they will be validated.

I have tried .send() and instance_variable_set but I am not sure if they will do the trick.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最舍不得你 2024-12-13 13:20:26

像这样的东西可能对你有用:

param_hash.each do |param, val|
  validator.instance_eval("def #{param}; @#{param} end")
  validator.instance_variable_set("@#{param}", val)
end

但是,你可能会注意到这里没有演员或任何东西。您需要以某种方式传达每个值的类型,因为例如不能假设“5”应该是整数。

当然,我可能不必提及,评估来自表单的输入并不完全是世界上最安全的事情,因此您必须考虑如何处理这个问题。

Something like this might work for you:

param_hash.each do |param, val|
  validator.instance_eval("def #{param}; @#{param} end")
  validator.instance_variable_set("@#{param}", val)
end

However, you might notice there's no casting or anything here. You'd need to communicate what type of value each is somehow, as it can't be assumed that "5" is supposed to be an integer, for example.

And of course I probably don't have to mention, eval'ing input that comes in from a form isn't exactly the safest thing in the world, so you'd have to think about how you want to handle this.

甚是思念 2024-12-13 13:20:26

您是否看过eval。只要您可以信任输入,就应该可以使用。

Have you looked at eval. As long as you can trust the inputs it should be ok to use.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文