1

I have 2 ruby files, Demo.rb and Config.rb.

In my Demo.rb file I'm requiring my Config.rb file:

require './Config' 

puts Config::test
puts Config::test2

Config.rb is as follows:

module Config
  # trying to add varibles/config details
  config1 = 'test'
  config2 = 'test2'
end

Now what I'm trying to do is have some variables in my Config module, and then be able to read those values from Demo.rb, but I keep getting an error. I've tried Config.test as well, but it keeps complaining about:

undefined method `user' for Config:Module

I was reading here: http://learnrubythehardway.org/book/ex40.html about it, but I seem to be doing what it's asking. Not sure where I'm going wrong.

2
  • The error message doesn't seem to match the code you've posted Commented Dec 2, 2015 at 21:59
  • I've fixed it, I was going through different names for it. Sorry about that. Commented Dec 2, 2015 at 22:05

1 Answer 1

2

This only works for constants. In Ruby, constants start with a capital letter:

module Config
  # trying to add varibles/config details
  Config1 = 'test'
  Config2 = 'test2'
end

puts Config::Config1
# => test

You could also define a module method:

module Config
  def self.config1
    'test'
  end
end

puts Config.config1
# => test
Sign up to request clarification or add additional context in comments.

Comments

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.