Ruby Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesRuby Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 24th, 2012, 08:27 PM
luis84 luis84 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 1 luis84 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 14 m 48 sec
Reputation 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

Reply With Quote
  #2  
Old July 4th, 2012, 04:56 AM
sepp2k1 sepp2k1 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Posts: 75 sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Day 23 h 20 m 52 sec
Reputation Power: 38
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesRuby Programming > Calling an RB File from another one

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap