ERB was used when I wrote the HTML generator the other day. This is the sentence when generating HTML:
Html = TPL. Result (binding)
The variable binding (a kernel method t_t) is a bit odd, so I searched it. It indicates the current scope of Ruby and does not have any externally visible member functions. The only purpose is to pass it to eval as the second parameter. So we can do this:
Def test_binding magic = 'Brother Chun is pure man' return bindingendeval "puts magic", test_binding
In this way, a scope is crossed.
Sometimes we can see the following constructor:
A = baby. New {name "makr" father "Mike" Age 0.2} A. Cry
The advantage is good-looking. It is also easy to implement. Use instance_eval:
Class baby def initialize (& BLC) instance_eval (& BLC) # Here end def name (STR = nil) @ name = STR if STR @ name end def age (num = nil) @ age = num if num @ age end def father (STR = nil) @ father = STR if STR @ Father end def cry puts "# {name} is only # {age. to_s} year old, he wanna milk! Brother Chun is pure man! "Endend
RepeatedCode? Use class_eval to shorten it, a bit like a macro:
Class baby def initialize (& BLC) instance_eval (& BLC) end def baby. my_attr (* names) names. each {| n | class_eval % {def # {n} (x = nil) # {n} = x if X # {n} end my_attr: Name,: Father,: Age def cry puts "# {name} is only # {age. to_s} year old, he wanna milk! Brother Chun is pure man! "Endend A = baby. New {name" makr "father" Mike "Age 0.2} A. Cry
Here, class_eval traverses the class scope and implements dynamic addition of functions. The instance_eval is also applied to the instance scope to modify its internal data. After understanding their traversal relationships, we can implement our class_eval and instance_eval-just get the binding from the appropriate place.
Class baby def my_instance_eval (CODE) eval code, binding end def baby. my_class_eval (code = '') eval code, binding endend
That's simple. The call is like this:
Class baby def initialize (CODE) my_instance_eval (CODE) end my_attr: name,: Father,: ageenda = baby. New % {name "test" father "orz" Age 0.2}
I just omitted one point, that is, class_eval and instance_eval can accept block replacement strings. After searching, it seems that the eval method to accept the block is not found. Therefore, this method can only be called the eval string class_eval.
Update: a small difference in the scope of Lambda and proc in Ruby, that is, the difference in binding. Proc directly uses the original binding. Lambda inherits the original scope to create a new binding.