send
Calling a method when method name is stored as a string object in a variable i.e. you can not see which method to call.
example 1
when method name is simply stored as a String object
- class C
- def wish
- p "hello world"
- end
- end
- a = "wish"
- c = C.new
- c.send(a)
example 2
making set method at runtime
- class C
- attr_accessor :name
- end
-
- c = C.new
-
- a = "name"
-
- c.send(a + "=", "Arun")
-
- p c.send(a)
example 3
this is interesting, when attribute name itself is send
- class C
- attr_accessor :send
- end
-
- c = C.new
-
- a = "send"
-
- c.__send__(a + "=", "Arun")
-
- p c.__send__(a) # => Arun



0 comments:
Post a Comment