Created
February 8, 2012 12:04
-
-
Save jimweirich/1768558 to your computer and use it in GitHub Desktop.
Vital Ruby Lab 6
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
class Module | |
def attr_flag(*names) | |
names.each do |name| | |
module_eval "def #{name}?(); @#{name}; end" | |
end | |
end | |
end |
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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
❤️