Ruby: Understanding _why's cloaker method -


i'm trying understand _why's cloaker method wrote in "a block costume":

class html   def cloaker &blk     (class << self; self; end).class_eval       # ... rest of method     end   end end 

i realise class << self; self; end opens eigenclass of self, i've never seen inside instance method before. self @ point this? under impression self should receiver method called on, cloaker called inside method_missing:

def method_missing tag, text = nil, &blk   # ...   if blk     cloaker(&blk).bind(self).call   end   # ... end 

so self inside method_missing call? , self when call:

((class << self; self; end).class_eval) 

inside cloaker method?

basically, want know whether we opening eignenclass of html class, or if doing specific instance of html class?

inside cloaker method, self instance of html since you'll call on object, you're creating singleton method on html class instances. instance:

class html   def cloaker &blk     (class << self; self; end).class_eval       def new_method       end     end   end end  obj = html.new obj.cloaker p html.methods.grep /new_method/  # [] p obj.singleton_methods # [:new_method] 

edit

or jörg w mittag commented , pre-1.9 way of calling "object#define_singleton_method"


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

What is the difference between data design and data model(ERD) -

ios - Can NSManagedObject conform to NSCoding -