same ruby file acting standalone resp. as library

Discussion in 'Ruby' started by Thomas Hafner, Apr 21, 2007.

  1. Hello,

    the class definitions of my ruby file shall always be active, whether
    the file is run as standalone program or whether the file is used
    indirectly by require or load,
    *but* when the file is run standalone, a special piece of code within
    the file shall be active in addition (a kind of main section).

    The idea is that the same ruby code file should be able to act in both
    kinds: as a library and as a standalone program which is using that
    library functionality.

    How can I achieve that easily?

    Regards
    Thomas Hafner
     
    Thomas Hafner, Apr 21, 2007
    #1
    1. Advertisements

  2. On Saturday 21 April 2007 16:25, Thomas Hafner wrote:
    > the file is run as standalone program or whether the file is used
    > indirectly by require or load,
    > *but* when the file is run standalone, a special piece of code within
    > the file shall be active in addition (a kind of main section).


    Perhaps you want:

    if __FILE__ == $0
    (do something)
    end

    as was just mentioned in another thread:

    http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/248563


    --
    Jesse Merriman

    http://www.jessemerriman.com/
     
    Jesse Merriman, Apr 21, 2007
    #2
    1. Advertisements

  3. Jesse Merriman <> wrote/schrieb <>:

    > Perhaps you want:
    >
    > if __FILE__ == $0
    > (do something)
    > end


    Yes, that's it.

    Thanks
    Thomas
     
    Thomas Hafner, Apr 21, 2007
    #3
    1. Advertisements

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. Tobias Merler
    Replies:
    0
    Views:
    504
    Tobias Merler
    Oct 9, 2004
  2. Replies:
    1
    Views:
    598
    Sudsy
    Dec 12, 2004
  3. Noah Roberts

    standalone xml library?

    Noah Roberts, Mar 24, 2005, in forum: C++
    Replies:
    6
    Views:
    1,544
    EventHelix.com
    Mar 25, 2005
  4. Ulf Meinhardt
    Replies:
    6
    Views:
    2,587
    Arne Vajhøj
    Jan 26, 2008
  5. Manish Buttan

    logging soap req./resp. while invoking webservices

    Manish Buttan, Apr 26, 2004, in forum: ASP .Net Web Services
    Replies:
    9
    Views:
    417
    [MSFT]
    May 6, 2004
  6. JeffP->

    WS Req/Resp as XML

    JeffP->, Sep 29, 2009, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    808
    JeffP->
    Sep 30, 2009
  7. Ion Frantzis

    Help ruby started acting strange

    Ion Frantzis, Nov 6, 2006, in forum: Ruby
    Replies:
    8
    Views:
    228
  8. Fahim Patel
    Replies:
    1
    Views:
    359
    Quintus
    Apr 22, 2014
Loading...