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="dave.burt, post: 4481781"] Constants are a kind of global variable. Classes get free constants. You can use something like the Singeton pattern: module XmlRpc def self.login(user, pass) @user = user @client = = XMLRPC::Client.new(...) @session_id = @client.call("xmlrpc.method", $user, $pass) end def self.user() @user or raise "Not initialized" end def self.client() @user or raise "Not initialized" end def self.session_id() @session_id or raise "Not initialized" end end Then you can login globally like so: XmlRpc.login("me", "my_password") Then elsewhere you can use: client = XmlRpc.client session_id = XmlRpc.session_id (Which will throw an exception if you're not logged in yet) What do you think? It's still global. Of course another idea is to use a single global, a Hash or OpenStruct, for these related data points: $xmlrpc[:session_id] Cheers, Dave [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Ruby
More elegant replacement for global variables
Top