ruby segundo ejercicio del tallerdemetaprogramacón
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby segundo ejercicio del tallerdemetaprogramacón相关的知识,希望对你有一定的参考价值。
module Atribulator
module ClassMethods
def my_attr_reader(name)
define_method("#{name}") do
instance_variable_get("@#{name}")
end
end
def my_attr_writer(name, my_type = nil)
if my_type.nil?
define_method("#{name}=") do |value|
instance_variable_set("@#{name}", value)
end
else
define_method("#{name}=") do |value|
raise ArgumentError unless value.is_a?(my_type)
instance_variable_set("@#{name}", value)
end
end
end
def my_attr_accessor(name, forced_type = nil)
my_attr_reader(name)
my_attr_writer(name, forced_type)
end
end
def self.included(klass)
klass.extend ClassMethods
end
end
class Two
include Atribulator
my_attr_accessor :foo, String
end
two = Two.new
two.foo = "bar"
puts "two.foo => #{two.foo}"
begin
two.foo = 2
rescue
puts "cazado!"
end
以上是关于ruby segundo ejercicio del tallerdemetaprogramacón的主要内容,如果未能解决你的问题,请参考以下文章