Last active
April 13, 2021 20:17
-
-
Save keithrbennett/1fb0f85d12f5943e79770a05018bd7b2 to your computer and use it in GitHub Desktop.
Illustrates that Ruby modules *can* have state but only on the module level, not the instance level.
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
#!/usr/bin/env ruby | |
module M | |
class << self | |
attr_accessor :foo | |
end | |
end | |
class C1 | |
def call | |
M.foo = 'C1 was here' | |
end | |
end | |
class C2 | |
def call | |
puts M.foo | |
end | |
end | |
C1.new.call | |
C2.new.call # => "C1 was here" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment