0

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

santuxus
  • 3,662
  • 1
  • 29
  • 35

2 Answers2

1

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.

WarHog
  • 8,622
  • 2
  • 29
  • 23
  • Hmmm - so which branch is main? Because https://github.com/binarylogic/settingslogic seems to be main, but it does not support array as source... – santuxus Nov 14 '11 at 11:42
  • Oops, it seems that I've made a blunder :( Please look at this: https://github.com/greghaygood/settingslogic. This is fork from greghaygood and he added support for multiple files. – WarHog Nov 14 '11 at 12:19
  • No problem. ;) This: https://github.com/siliconsalad/settingslogic also works - the difference is that greghaygood uses args* and the second solution array explicitly. Now it's been changed to string concatenation too. I just don't get why "... #{@section}" in this code was ok in ruby 1.8 and it's not working in ruby 1.9.2... – santuxus Nov 14 '11 at 13:13
0

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.

megas
  • 21,401
  • 12
  • 79
  • 130
  • No, this part of code intended for working with `Class` object and define methods inside it. – WarHog Nov 09 '11 at 15:27
  • I checked my answer by this code "class Test;self.class_eval("def test; puts 'test'; end");end". It creates test method for Test class and then i can derive object with this method. Isn't that you want? – megas Nov 09 '11 at 18:34
  • This wasn't my question :) I meant `self.class.class_eval` is part of `settingslogic` and problem isn't linked with this part of code. Author of this library want to add methods (evaluated through `def #{key}`) to Class object, not to Test object, so that these methods become class methods of any class (Test.test, Object.test etc.). It's rather strange, but its up to author of this gem ;) – WarHog Nov 09 '11 at 19:17