Mega Code Archive

 
Categories / Ruby / Method
 

Use method from String class to map a method from String to a method pointer

str = "cat" meth = str.method(:length) a = meth.call               #  3  (length of "cat") str << "erpillar" b = meth.call               # 11  (length of "caterpillar") str = "dog" c = meth.call               # 11  (length of "caterpillar")