Created
December 6, 2010 16:11
-
-
Save boriscy/730495 to your computer and use it in GitHub Desktop.
Ruby dsl
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 Prueba | |
attr_reader :nombre, :apellido, :edad | |
def initialize(&b) | |
instance_eval &b | |
end | |
def method_missing(method, param) | |
method = method.to_s | |
if prueba_methods.include?(method) | |
self.instance_eval <<-MEM, __FILE__, __LINE__ + 1 | |
def #{method}(val) | |
@#{method.gsub(/set_/, '')} = val | |
end | |
MEM | |
self.send(method.to_sym, param) | |
end | |
end | |
def prueba_methods | |
%w(nombre apellido edad).map { |v| "set_#{v}" } | |
end | |
end | |
pru = Prueba.new do | |
set_nombre 'Carlos' | |
set_apellido 'Ramos' | |
set_edad 26 | |
end | |
p pru.nombre | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment