Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Ruby
More elegant replacement for global variables
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Michael, post: 4481778"] I'm playing around with a ruby app using XML-RPC. Currently I'm using global variables so I can create multiple objects without having to log in for each and every object. class Base def initialize(user, pass) $user, $pass = user, pass end def login() $client = XMLRPC::Client.new(...) $sessionid = $client.call("xmlrpc.method", $user, $pass) return $sessionid end end Derived classes look like this: class Object1 < Base def initialize() if not $sessionid then self.login end end end This way I can log in once with the first object created without having to log in each time. However, this seems inelegant to me. Is there a more elegant, or just more rubyish, way of doing this? Also, I'd have potential problems if I wanted to log on as multiple users. Would it make more sense just to pass around an instance of the Base object: class Object2 def initialize(xmlrpc_client) if not xmlrpc_client.sessionid then xmlrpc_client.login end def call_xmlrpc_method(method, params) results = xmlrpc_client.call(method, params) return results end end xmlrpc_client = Base.new o2 = Object2.new(xmlrpc_client) Pro: no global variables. Con: coupling between Object2 and Base classes. --Michael [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Ruby
More elegant replacement for global variables
Top