Hi, I was experimenting with the arguments of the methods and I have a
question:
As I can pass a method as an argument to another method?, For example:
def world
print ‘world!’
end
def aaa (string, method)
print string
end
aaa (‘hello’, world)
When I invoke aaa, the method world should not be invoked because I’m
just passing it as argument and and neither I invoke this in the method
aaa, and yet it invokes!. Why is this?.
I welcome your support!. Sorry for bad english 
there are two ways:
pass it as symbol:
aaa(‘hello’, :world)
or pass it as method object
aaa(‘hello’, method(:world))
depend on what you want todo
PS: no " " before the “(” or you get many trouble
In method call you can omit parenthnes. So this is equivalent to
def world()
print(‘world!’)
end
def aaa (string, method)
print(string)
end
aaa (‘hello’, world())
On Fri, May 18, 2012 at 05:10:14PM +0900, Robinson R. wrote:
def aaa (string, method)
I welcome your support!. Sorry for bad english 
–
Posted via http://www.ruby-forum.com/.
–
Electrical conduits in machine room are melting.
You would have to implement def aaa a little differently.
If you want to call it as aaa(‘hello’, :world): (note: this is
generally a bad idea)
def aaa(string, method)
print string
send method
end
Note the “send” – it call the method whose name is given as argument
on “self”; in this case (with toplevel methods) it will be an implicit
toplevel object. (If either of your methods belonged to some class,
this would stop working – that’s why it’s a bad idea.)
If you want to call it as aaa(‘hello’, method(:world)):
def aaa(string, method)
print string
method.call
end
Here, “method” is an object which acts just like a Proc, so it can be
called.
– Matma R.
Hans M. wrote in post #1061260:
there are two ways:
pass it as symbol:
aaa(‘hello’, :world)
or pass it as method object
aaa(‘hello’, method(:world))
depend on what you want todo
PS: no " " before the “(” or you get many trouble
If I pass the method world as a symbol, certainly this is not executed
in the arguments, But nor is running if I invoke in the method aaa.
example:
def world
print ‘world!’
end
def aaa(string, method)
print string
method #INVOKING THE METHOD
end
aaa(‘hello’, :world)
What I can do?.
Bartosz Dziewoński wrote in post #1061310:
You would have to implement def aaa a little differently.
If you want to call it as aaa(‘hello’, :world): (note: this is
generally a bad idea)
def aaa(string, method)
print string
send method
end
Note the “send” – it call the method whose name is given as argument
on “self”; in this case (with toplevel methods) it will be an implicit
toplevel object. (If either of your methods belonged to some class,
this would stop working – that’s why it’s a bad idea.)
If you want to call it as aaa(‘hello’, method(:world)):
def aaa(string, method)
print string
method.call
end
Thanks!, now the method world is invoked exactly as I wanted, in the aaa
method only. With either of two ways (send or call) works perfectly!.
Although I prefer to pass the method as an object and then invoke with
call.
Just to make sure my head’s on straight, the method is still bound to
its original object, right? Any @instancevars in there will still
refer to the right instance?
On 19 May 2012 11:28, Robinson R. [email protected] wrote:
end
print string
method.call
end
Thanks!, now the method world is invoked exactly as I wanted, in the aaa
method only. With either of two ways (send or call) works perfectly!.
Although I prefer to pass the method as an object and then invoke with
call.
–
Posted via http://www.ruby-forum.com/.
–
Matthew K., B.Sc (CompSci) (Hons)
http://matthew.kerwin.net.au/
ABN: 59-013-727-651
“You’ll never find a programming language that frees
you from the burden of clarifying your ideas.” - xkcd
Robinson R. wrote in post #1061259:
Hi, I was experimenting with the arguments of the methods and I have a
question:
As I can pass a method as an argument to another method?, For example:
If you only want to have an ad hoc function to use in the main method,
you
may pass a block instead. This is used much more often than passing
actual methods (or symbols as method names).
On Sat, May 19, 2012 at 6:05 AM, Matthew K. [email protected]
wrote:
Just to make sure my head’s on straight, the method is still bound to
its original object, right? Any @instancevars in there will still
refer to the right instance?
Yes, check this:
1.9.2p290 :001 > class A
1.9.2p290 :002?> def initialize
1.9.2p290 :003?> @a = 3
1.9.2p290 :004?> end
1.9.2p290 :007?> def m
1.9.2p290 :008?> puts @a
1.9.2p290 :009?> end
1.9.2p290 :010?> def get_me_the_method
1.9.2p290 :011?> method(:m)
1.9.2p290 :012?> end
1.9.2p290 :013?> end
=> nil
1.9.2p290 :014 > the_method = A.new.get_me_the_method
=> #<Method: A#m>
1.9.2p290 :015 > the_method.call
3
=> nil
You can also have Unbound methods:
http://www.ruby-doc.org/core-1.9.3/UnboundMethod.html
but you have to bind them to some instance before calling them.
Jesus.