Skip to content

Instantly share code, notes, and snippets.

@3limin4t0r
Last active January 10, 2020 10:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 3limin4t0r/56ba719b2c72fbebaaa33099b103ad02 to your computer and use it in GitHub Desktop.
Save 3limin4t0r/56ba719b2c72fbebaaa33099b103ad02 to your computer and use it in GitHub Desktop.
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