1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Rep Power

    Calling an RB File from another one


    I have a main RB file (gem) that runs, it's a siriproxy server. Basically, it runs and listens to commands and executes script when it catches matching text.

    So I am now using an rb file seperately, which returns my paypal balance. So basically, paypal.rb when execute simply returns a number, a paypal balance.

    I need to get the number that paypal.rb returns into the initial script.

    Is it possible to do this and how would I go about that? I was thinking in my main program I can do "require paypal.rb"

    and then I could call the command but I am not sure how?

    Any help would be great.

    The paypal.rb file contains this code,
    require 'ruby-paypal'
    username = 'xx'
    password = 'xx'
    signature = 'xx'
    paypal = Paypal.new username, password, signature, :production
    # silence annoying output
    class CaptureOutput < IO
        def initialize
        def write(text)
            # send text to logfile
    def silence
        raise unless block_given?
        dout, serr, sout = $defout, $stderr, $stdout
        buf = CaptureOutput.new
            $defout = buf
            $stderr = buf
            $stdout = buf
            $defout, $stderr, $stdout = dout, serr, sout
    silence do
      paypal = paypal.make_nvp_call 'METHOD' => 'GetBalance'
    if paypal.ack == 'Success'
      puts paypal['L_AMT0']
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Rep Power
    When you say you want to get the number that your script returns, you mean the number that it prints, right?

    The quickest way to achieve that would probably be to invoke it inside backticks, i.e. do foo = `ruby paypal.rb`.to_i inside your main script. However that's not a very clean solution.

    The clean solution would be to change your paypal.rb, so that the code to get the number is inside a method. Then you can require the file and just call the method to get at the number.

    If your script still needs to be runnable from the command line (in which case the number should of course be printable), you can add a piece of code that calls the method and prints the result inside a "if __FILE__ == $0" block, which is the ruby way to define a piece of code that should only run when the script is run from the command line and not when it is required by another file.

IMN logo majestic logo threadwatch logo seochat tools logo