--------------020409050801010005050908
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Here's a better version of my solution. Obviously these modules would
have to be included in Object to work. The names should be self
explainitory. All lines are less than 80 chars.
module Attribute20PercentGolfed
def attribute(arg, &b)
return attribute(arg.keys[0]) { arg.values[0] } if arg.class == Hash
define_method(arg) do
init = block_given? && !instance_eval("defined? "+"@"+arg)
init ? send(arg+"=",instance_eval(&b)) :
instance_variable_get("@"+arg)
end
define_method(arg+"?") { !!send(arg) }
attr_writer(arg)
end
end
module Attribute75PercentGolfed; def attribute(a, &b)
return attribute(a.keys[0]) { a.values[0] } if a.class == Hash
define_method(a) { block_given? && !instance_eval("defined? "+"@"+a) ?
send(a+"=",instance_eval(&b)):instance_variable_get("@"+a) }
define_method(a+"?") { !!send(a) } && attr_writer(a); end; end
--------------020409050801010005050908
Content-Type: text/plain;
name="knowledge.rb"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="knowledge.rb"
module Attribute20PercentGolfed
def attribute(arg, &b)
return attribute(arg.keys[0]) { arg.values[0] } if arg.class == Hash
define_method(arg) do
init = block_given? && !instance_eval("defined? "+"@"+arg)
init ? send(arg+"=",instance_eval(&b)) : instance_variable_get("@"+arg)
end
define_method(arg+"?") { !!send(arg) }
attr_writer(arg)
end
end
module Attribute75PercentGolfed; def attribute(a, &b)
return attribute(a.keys[0]) { a.values[0] } if a.class == Hash
define_method(a) { block_given? && !instance_eval("defined? "+"@"+a) ?
send(a+"=",instance_eval(&b)):instance_variable_get("@"+a) }
define_method(a+"?") { !!send(a) } && attr_writer(a); end; end
--------------020409050801010005050908--