From: Alex Young Date: 2007-07-31T06:42:42+09:00 Subject: Re: Passing variables to a required file 12 34 wrote: > Alex Young wrote: >> 12 34 wrote: >>> Is there a better way? FWIW I'm "require'ing a modified example.rb file >>> that is included in the Pashua app for creating a simple GUI for OS X. >>> >> Change your code. It'll be easier to do it now than to unpick >> everything later. >> >> What you want (probably) is to wrap your code in the require'd file into >> a class that you can instantiate with the relevant parameters in your >> main script. >> > > OK. For now that's the best solution for me, but later I will look at > putting in a separate file. Well, the class definition can still be in the external file - that's actually what I meant. You can do something like this: main.rb: require 'ui_code' UICode.new(all, your, parameters).run ui_code.rb: class UICode def initialize(use, meaningful, parameter, names) @use = use @meaningful = meaningful # etc... end def run #your current code goes here, referring to instance variables rather # than globals end end That's a fairly typical arrangement, in fact. >>> something in how it's used. >> It's a heredoc marker. It's just another way of creating a multiline >> string. The critical bit is the "<> can put any identifier, and a string will be created from the content >> between that line and the first occurrence of that marker on its own >> later in the file. > > Thank you Alex and Thomas, now I see that a long string is being > prepared to send to the Perl script or something like that. And I'm > trying to get my Ruby variable in that string. They syntax in the string > didn't quite look like Ruby, missing quotes and things. > > So EOS presumably means something like End of String and is commonly > used but has no special meaning to Ruby. Confusing also to me that the > word in front of < name or word with a special meaning. Nope, it's just a variable name. Except that here it's a little weird - because it's capitalised, it's a constant. Not sure if that's relevant, but don't worry about the specific name "Config" being anything special. -- Alex