-
-
Save 3limin4t0r/56ba719b2c72fbebaaa33099b103ad02 to your computer and use it in GitHub Desktop.
This file contains 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
module Foo | |
@some_variable = 'default value' | |
# https://stackoverflow.com/questions/2505067/class-self-idiom-in-ruby | |
class << self | |
# Allow setting through: | |
# | |
# Foo.some_variable = 'some value' | |
# | |
# and getting through: | |
# | |
# Foo.some_variable | |
# | |
# You can also use #attr_reader or #attr_writer if you only | |
# want a getter or setter. | |
# | |
# https://ruby-doc.org/core-2.7.0/Module.html#method-i-attr_accessor | |
attr_accessor :some_variable | |
def some_method | |
# use the getter to retrieve the value of @some_variable | |
puts "do something with #{some_variable}" | |
end | |
end | |
end | |
Foo.some_method | |
# prints: do something with default value | |
Foo.some_variable = 'configured value' | |
Foo.some_method | |
# prints: do something with configered value |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment