Use method from String class to map a method from String to a method pointer : Method Pointer « Method « Ruby






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")

 








Related examples in the same category

1.Call String.instance_method to get a method pointer
2.Get pointer to a method in Fixnum
3.Assign method pointer to another method