Passing variables to a required file

Discussion in 'Ruby' started by 12 34, Jul 30, 2007.

  1. 12 34

    12 34 Guest

    I want to pass about 10 variables to a "require"d file. My understanding
    is that I need to use global variables. Is this correct?

    I'm reluctant to change all the variables to global for fear of messing
    up the methods in the script. I can of course define new global
    variables equal to the local variable. Seems inelegant.

    I guess my best option is to rework the require'd file as a method, but
    my script is getting long and keeping the file separate makes sense,
    particularly since it is creating the GUI, so is different from the
    script function if I understand the MVC philosophy.

    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.
    <http://www.bluem.net/downloads/pashua_en/>

    Thanks for info

    PS The example file as a

    Config = <<EOS

    <bunch of code>
    EOS

    I can't find EOS in Ruby on Rails or PickAxe, so maybe I'm missing
    something in how it's used.
    --
    Posted via http://www.ruby-forum.com/.
     
    12 34, Jul 30, 2007
    #1
    1. Advertising

  2. 12 34

    Alex Young Guest

    12 34 wrote:
    > I want to pass about 10 variables to a "require"d file. My understanding
    > is that I need to use global variables. Is this correct?
    >
    > I'm reluctant to change all the variables to global for fear of messing
    > up the methods in the script. I can of course define new global
    > variables equal to the local variable. Seems inelegant.
    >
    > I guess my best option is to rework the require'd file as a method, but
    > my script is getting long and keeping the file separate makes sense,
    > particularly since it is creating the GUI, so is different from the
    > script function if I understand the MVC philosophy.
    >
    > 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.
    > <http://www.bluem.net/downloads/pashua_en/>

    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.

    > Thanks for info
    >
    > PS The example file as a
    >
    > Config = <<EOS
    >
    > <bunch of code>
    > EOS
    >
    > I can't find EOS in Ruby on Rails or PickAxe, so maybe I'm missing
    > 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 "<<EOS" start marker - after "<<" you
    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.

    HTH
    --
    Alex
     
    Alex Young, Jul 30, 2007
    #2
    1. Advertising

  3. 2007/7/30, 12 34 <>:
    > I want to pass about 10 variables to a "require"d file. My understanding
    > is that I need to use global variables. Is this correct?
    >


    I'd do the same as Alex and put the Config constant in a class(or any
    other way to pass the parameters, I am quite new to Ruby and don't
    know of something better yet)

    >
    > PS The example file as a
    >
    > Config = <<EOS
    >
    > <bunch of code>
    > EOS
    >
    > I can't find EOS in Ruby on Rails or PickAxe, so maybe I'm missing
    > something in how it's used.
    >


    Check Chapter 5 "Strings" in the Pickaxe, it is called *here document* there.
     
    Thomas Wieczorek, Jul 30, 2007
    #3
  4. 12 34

    12 34 Guest

    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.
    >> <http://www.bluem.net/downloads/pashua_en/>

    > 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.

    >> 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 "<<EOS" start marker - after "<<" you
    > 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 <<EOS was "Config" which could be just a descriptive
    name or word with a special meaning.
    >
    > HTH


    I think this will get me over the hump of using a file to prepare a
    string to send to some kind of Perl script (it's really an OS X app) to
    make a GUI. At the moment I was trying to recover the return from
    calling the "require." That problem will probably go away if I make it
    class or method.
    --
    Posted via http://www.ruby-forum.com/.
     
    12 34, Jul 30, 2007
    #4
  5. 12 34

    Alex Young Guest

    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.
    >>> <http://www.bluem.net/downloads/pashua_en/>

    >> 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 "<<EOS" start marker - after "<<" you
    >> 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 <<EOS was "Config" which could be just a descriptive
    > 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
     
    Alex Young, Jul 30, 2007
    #5
  6. 12 34

    12 34 Guest

    Alex Young wrote:
    > 12 34 wrote:
    >>> 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.


    Thank you for clarifying this. Since it's a one shot thing I've been
    using defs and not classes. But maybe that makes a difference. I need to
    reread my books to get clearer on this.
    >
    >> 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 <<EOS was "Config" which could be just a descriptive
    >> 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.


    Turns out to matter a lot. Broken with EOS, works with eos. Although did
    worked when in a separate file. I had moved it into my main script. I
    came back to the thread to repost about an error I was getting at the
    <<EOS. So that's fixed.

    Thanks for your comments.
    --
    Posted via http://www.ruby-forum.com/.
     
    12 34, Jul 31, 2007
    #6
  7. 12 34

    12 34 Guest

    12 34 wrote:
    >>> 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 <<EOS was "Config" which could be just a descriptive
    >>> 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.

    >
    > Turns out to matter a lot. Broken with EOS, works with eos. Although did
    > worked when in a separate file. I had moved it into my main script. I
    > came back to the thread to repost about an error I was getting at the
    > <<EOS. So that's fixed.
    >

    Sorry, my response above is incorrect. I made the change from EOS to
    eos in the stand alone file and ran that file in TextMate. When I use it
    as a require or integrated in my main script I get the following error
    (changed from eos to end_of_string).

    dynamic constant assignment
    Config = <<end_of_string

    Maybe I'd better go back to the separate file and start over again.
    --
    Posted via http://www.ruby-forum.com/.
     
    12 34, Jul 31, 2007
    #7
  8. 2007/7/30, 12 34 <>:
    > I want to pass about 10 variables to a "require"d file. My understanding
    > is that I need to use global variables. Is this correct?
    >
    > I'm reluctant to change all the variables to global for fear of messing
    > up the methods in the script. I can of course define new global
    > variables equal to the local variable. Seems inelegant.
    >
    > I guess my best option is to rework the require'd file as a method, but
    > my script is getting long and keeping the file separate makes sense,
    > particularly since it is creating the GUI, so is different from the
    > script function if I understand the MVC philosophy.
    >
    > 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.
    > <http://www.bluem.net/downloads/pashua_en/>
    >
    > Thanks for info
    >
    > PS The example file as a
    >
    > Config = <<EOS
    >
    > <bunch of code>
    > EOS
    >
    > I can't find EOS in Ruby on Rails or PickAxe, so maybe I'm missing
    > something in how it's used.


    I'd suggest to refactor your code so you have multiple classes and can
    instantiate them with appropriate parameters. Storing code in a long
    string is a bad option if the code is known at compile time which
    seems to be the case here.

    Kind regards

    robert
     
    Robert Klemme, Jul 31, 2007
    #8
  9. 12 34

    12 34 Guest

    Robert Klemme wrote:
    > 2007/7/30, 12 34 <why does my email appear here ARGH>:
    >> EOS
    >>

    > I'd suggest to refactor your code so you have multiple classes and can
    > instantiate them with appropriate parameters. Storing code in a long
    > string is a bad option if the code is known at compile time which
    > seems to be the case here.
    >
    > Kind regards
    >
    > robert


    This is getting over my head, but the code that is a string will be
    changed on running the script. I'll be putting in variable via
    #{variable}. As I understand it this string is sent to a Mac application
    that puts up a GUI window and then returns the options (text, filenames,
    etc) entered in a hash. I'm using the initial #{variable} to put in
    default selections in the GUI. They could be written in the original
    script, but I'd rather keep them in the main script. And I envision
    transitioning to other GUI's if this one doesn't work out or I learn
    Rails or who know what?

    If I should still do as you say, let me know and I'll study some more.
    As I may have said earlier, I've only used defs (and I use them
    liberally for what I think is the usual reason to keep the code easier
    to read). Although I've worked through some tutorials and Ruby on Rails
    stuff, I haven't quite got my head around classes and instantiation. My
    script / app will be run each time I download photos and set them up for
    transfer to my photo program (Aperture). I'm not saving any of the
    information except defaults which at the moment I'm hard wiring into the
    code, but may set up a YAML to save the last selections which then
    become the defaults. I've also set myself the task of (re)reading
    PickAxe on my vacation which starts next week.

    Thank you for your comments.
    --
    Posted via http://www.ruby-forum.com/.
     
    12 34, Jul 31, 2007
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Just D.
    Replies:
    0
    Views:
    462
    Just D.
    Mar 10, 2006
  2. Replies:
    9
    Views:
    1,007
  3. Alan
    Replies:
    4
    Views:
    266
    Bill Felton
    Jan 27, 2011
  4. Andy
    Replies:
    1
    Views:
    170
    Eidolon
    May 7, 2004
  5. Ian Hobson
    Replies:
    0
    Views:
    113
    Ian Hobson
    Nov 27, 2003
Loading...

Share This Page