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

    Join Date
    Jun 2012
    Posts
    1
    Rep Power
    0

    Calling an RB File from another one


    Hello,

    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,
    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
            super(2)
        end
        def write(text)
            # send text to logfile
        end
    end
    def silence
        raise unless block_given?
        dout, serr, sout = $defout, $stderr, $stdout
        buf = CaptureOutput.new
        begin
            $defout = buf
            $stderr = buf
            $stdout = buf
            yield
        ensure
            $defout, $stderr, $stdout = dout, serr, sout
        end
    end
    
    silence do
      paypal = paypal.make_nvp_call 'METHOD' => 'GetBalance'
    end
    
    if paypal.ack == 'Success'
      puts paypal['L_AMT0']
    end
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    83
    Rep Power
    39
    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