Created
May 31, 2017 03:03
-
-
Save charlespeach/86a3dbc455a6ba282ae87b31ca04e443 to your computer and use it in GitHub Desktop.
Global variable using ruby singleton pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SQSToken | |
class << self | |
def get_token | |
puts @@sqs_token ||= magic_method_which_gets_the_token_the_first_time | |
end | |
# Replace this method with a call to what ever magic | |
# code that gets a new token only if required | |
def magic_method_which_gets_the_token_the_first_time | |
puts 'ive run!' | |
"magic string!" | |
end | |
end | |
end | |
SQSToken.get_token # 1st run it will set the token and set a class variable | |
SQSToken.get_token # 2nd run it will return the previously set variable | |
# ruby sqs.rb | |
# ive run! | |
# magic string! | |
# magic string! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment