Getting access to variables in file

4 views
Skip to first unread message

Nick Skriloff

unread,
Nov 23, 2015, 10:03:13 AM11/23/15
to rub...@googlegroups.com
Suppose that I have
fileA.rb and fileB.rb

FileA.rb has
require "./fileB"
require "minitest/autorun"

fileB.rb has
String SOME_CONSTANT = "test"
class MakeCommandFiles
String BASE_DIR_EXPANDED = File.expand_path(File.dirname(__FILE__));

def makeFirstCommandFile()

end
end

In FileA.rb I could access the constant in fileB.rb by the class name like so
MakeCommandFiles::BASE_DIR_EXPANDED
 
How do I access SOME_CONSTANT from fileA.rb? 


Sincerely, Nick Skriloff

Keith Bennett

unread,
Nov 23, 2015, 11:22:51 AM11/23/15
to rubyjam
Nick -

The SOME_CONSTANT is defined at the top level outside of any explicit class, so you don't need to preface it with anything. If you run these from the command line it will illustrate:

echo "FOO = 'hello'" > b.rb

echo "require_relative 'b'" > a.rb

echo "puts FOO" >> a.rb

ruby a.rb


- Keith




--
You received this message because you are subscribed to the Google Groups "Ruby Code Jam" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyjam+u...@googlegroups.com.
To post to this group, send email to rub...@googlegroups.com.
Visit this group at http://groups.google.com/group/rubyjam.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages