Send an unknown method call: Difference between revisions
Content added Content deleted
(Task rewrite but maintaining original intent.) |
m (Python < Ruby. Link to runtime evaluation.) |
||
Line 5: | Line 5: | ||
;Cf: |
;Cf: |
||
* [[Respond to an unknown method call]]. |
* [[Respond to an unknown method call]]. |
||
* [[Runtime evaluation]] |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
⚫ | |||
<lang ruby> |
|||
⚫ | |||
def foo |
def foo |
||
42 |
42 |
||
Line 21: | Line 28: | ||
Example.new.send symbol # => 42 |
Example.new.send symbol # => 42 |
||
Example.new.send( :bar, 1, 2 ) { |x,y| x+y } # => 3</lang> |
Example.new.send( :bar, 1, 2 ) { |x,y| x+y } # => 3</lang> |
||
⚫ | |||
⚫ | |||
<lang python> |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ |
Revision as of 22:53, 27 August 2011
Send an unknown method call is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Invoke an object method where the name of the method to be invoked can be generated at run time.
- Cf
Python
String literal "foo" may be replaced by any expression resulting in a string <lang python>class Example(object):
def foo(self): return 42
getattr(Example(), "foo")() # => 42</lang>
Ruby
<lang ruby>class Example
def foo 42 end def bar(arg1, arg2, &block) block.call arg1, arg2 end
end
symbol = :foo Example.new.send symbol # => 42 Example.new.send( :bar, 1, 2 ) { |x,y| x+y } # => 3</lang>