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

2
  • What is @section == ["User/project/config/defaults.yml", "/Users/project/config/development.yml"] all about? Commented Nov 9, 2011 at 16:50
  • just a shortcut to show what is the value of @section at the moment... Commented Nov 14, 2011 at 11:41

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Hmmm - so which branch is main? Because github.com/binarylogic/settingslogic seems to be main, but it does not support array as source...
Oops, it seems that I've made a blunder :( Please look at this: github.com/greghaygood/settingslogic. This is fork from greghaygood and he added support for multiple files.
No problem. ;) This: 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...
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.

3 Comments

No, this part of code intended for working with Class object and define methods inside it.
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?
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 ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.