Ruby 1.9.2 class_eval 变量

发布于 2024-12-15 03:03:48 字数 1683 浏览 4 评论 0原文

Settingslogic fork 允许数组作为源中,在 ruby​​ 1.8.7 中一切正常,但在 ruby​​ 1.9.2 中有错误。问题出在这部分代码中:

self.class.class_eval <<-EndEval
  def #{key}
    return @#{key} if @#{key}
    raise MissingSetting, "Missing setting '#{key}' in #{@section}" unless has_key? '#{key}'
    value = fetch('#{key}')
    @#{key} = value.is_a?(Hash) ? self.class.new(value, "'#{key}' section in #{@section}") : value
  end
EndEval

@section == ["path_to_yml_file1", "path_to_yml_file2",...]

看起来 #{} 以某种奇怪的方式求值,“#{@section}”似乎是一个数组,而不是一个字符串。有人能解释一下吗?

错误跟踪:

@section == ["User/project/config/defaults.yml", "/Users/project/config/development.yml"]


ruby-1.9.2-p290 :001 > Settings.keys
SyntaxError: (eval):3: syntax error, unexpected tSTRING_BEG, expecting keyword_end
...project/config/defaults.yml", "/Users/project...
...                               ^
(eval):3: syntax error, unexpected tSTRING_BEG, expecting keyword_end
...project/config/development.yml"]" unless has_key? 'front'
...                               ^
(eval):5: syntax error, unexpected tSTRING_BEG, expecting ')'
...project/config/defaults.yml", "/Users/project...
...                               ^
(eval):5: syntax error, unexpected tSTRING_BEG, expecting keyword_end
...project/config/development.yml"]") : value
...                               ^
(eval):5: syntax error, unexpected ')', expecting keyword_end
...project/config/development.yml"]") : value
...                               ^

from .../settingslogic-3b5d7d9cc319/lib/settingslogic.rb:198:in `class_eval'

感谢您的帮助

In Settingslogic fork allowing array as source, in ruby 1.8.7 everything is working, but in ruby 1.9.2 there is an error. The problem is within this part of the code:

self.class.class_eval <<-EndEval
  def #{key}
    return @#{key} if @#{key}
    raise MissingSetting, "Missing setting '#{key}' in #{@section}" unless has_key? '#{key}'
    value = fetch('#{key}')
    @#{key} = value.is_a?(Hash) ? self.class.new(value, "'#{key}' section in #{@section}") : value
  end
EndEval

@section == ["path_to_yml_file1", "path_to_yml_file2",...]

Looks like #{} is evaluated in some strange way, "#{@section}" seems to be an array, not a string. Can anybody explain this?

Error trace:

@section == ["User/project/config/defaults.yml", "/Users/project/config/development.yml"]


ruby-1.9.2-p290 :001 > Settings.keys
SyntaxError: (eval):3: syntax error, unexpected tSTRING_BEG, expecting keyword_end
...project/config/defaults.yml", "/Users/project...
...                               ^
(eval):3: syntax error, unexpected tSTRING_BEG, expecting keyword_end
...project/config/development.yml"]" unless has_key? 'front'
...                               ^
(eval):5: syntax error, unexpected tSTRING_BEG, expecting ')'
...project/config/defaults.yml", "/Users/project...
...                               ^
(eval):5: syntax error, unexpected tSTRING_BEG, expecting keyword_end
...project/config/development.yml"]") : value
...                               ^
(eval):5: syntax error, unexpected ')', expecting keyword_end
...project/config/development.yml"]") : value
...                               ^

from .../settingslogic-3b5d7d9cc319/lib/settingslogic.rb:198:in `class_eval'

Thanks for any help

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

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

发布评论

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

评论(2

优雅的叶子 2024-12-22 03:03:48

您已经从主 settingslogic 中创建了一个分支。当时它不支持数组作为源,但现在支持了。尝试使用主 settingslogic 存储库。

您的错误现在与此字符串相关:

raise MissingSetting,
  "Missing setting '#{key}' in #{@section}" unless has_key? '#{key}'

因为在使用数组而不是字符串的情况下,

./settings.yml

您会得到类似这样的结果:

[\"./settings.yml\"]

下面的 @#{key} 赋值也会发生同样的情况。在主存储库中,此代码替换为字符串连接。

You've made a fork from main settingslogic. At that time it didn't support array as source, but now it does. Try to use main settingslogic repository.

Your error now related to this string:

raise MissingSetting,
  "Missing setting '#{key}' in #{@section}" unless has_key? '#{key}'

because in case of using array instead of string

./settings.yml

you get something like this:

[\"./settings.yml\"]

The same happens with @#{key} assignment below. In main repository this code replaced to string concatenation.

暮年 2024-12-22 03:03:48

尝试self.class_eval或者甚至没有self,不需要获取类的名称,self会自动分配给当前对象,即您的类。

Try self.class_eval or even without self, no need to get the name of class and self automatically assign to current object i.e. your class.

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