Skip to content

Instantly share code, notes, and snippets.

@boriscy
Created December 6, 2010 16:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boriscy/730495 to your computer and use it in GitHub Desktop.
Save boriscy/730495 to your computer and use it in GitHub Desktop.
Ruby dsl
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