Skip to content

Instantly share code, notes, and snippets.

@leejarvis
Last active April 13, 2017 16:32
Show Gist options
  • Save leejarvis/5d8514287ed5e757b967e5bd4917774e to your computer and use it in GitHub Desktop.
Save leejarvis/5d8514287ed5e757b967e5bd4917774e to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
module ClassInstanceVariables
def class_instance_variable(name, value = nil, private: true)
(class << self; self; end).instance_eval {
attr_accessor name
private name if private
}
public_send("#{name}=", value)
attr_writer name
define_method(name) { value }
private name if private
end
end
class Foo
extend ClassInstanceVariables
class_instance_variable :foo
class_instance_variable :baz, 87, private: false
end
raise unless Foo.send(:foo) == nil
raise unless Foo.new.send(:foo) == nil
raise unless Foo.baz == 87
raise unless Foo.new.baz == 87
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment