Ruby 1.9.2 class_eval 变量
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您已经从主
settingslogic
中创建了一个分支。当时它不支持数组作为源,但现在支持了。尝试使用主settingslogic
存储库。您的错误现在与此字符串相关:
因为在使用数组而不是字符串的情况下,
您会得到类似这样的结果:
下面的
@#{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 mainsettingslogic
repository.Your error now related to this string:
because in case of using array instead of string
you get something like this:
The same happens with
@#{key}
assignment below. In main repository this code replaced to string concatenation.尝试
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.