Skip to content

Instantly share code, notes, and snippets.

@jimweirich
Created February 8, 2012 12:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save jimweirich/1768558 to your computer and use it in GitHub Desktop.
Save jimweirich/1768558 to your computer and use it in GitHub Desktop.
Vital Ruby Lab 6
class Module
def attr_flag(*names)
names.each do |name|
module_eval "def #{name}?(); @#{name}; end"
end
end
end
require 'test/unit'
require 'attr_flag'
class AttrFlagTest < Test::Unit::TestCase
class X
attr_accessor :ok
attr_flag :ok
attr_flag :a, :b, :c
def initialize
@a = @b = @c = true
end
end
def test_flag_is_true
x = X.new
x.ok = true
assert x.ok?
end
def test_flag_is_false
x = X.new
x.ok = false
assert ! x.ok?
end
def test_defines_multiple_names
x = X.new
assert x.a?
assert x.b?
assert x.c?
end
end
@jxa
Copy link

jxa commented Nov 17, 2017

❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment