Skip to content

Instantly share code, notes, and snippets.

@keithrbennett
Last active April 13, 2021 20:17
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 keithrbennett/1fb0f85d12f5943e79770a05018bd7b2 to your computer and use it in GitHub Desktop.
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.
#!/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