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?.
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 Risquez wrote:
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?.
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 method 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).
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.
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 Risquez <lists@ruby-forum.com> wrote:
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.
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:
but you have to bind them to some instance before calling them.
Jesus.
···
On Sat, May 19, 2012 at 6:05 AM, Matthew Kerwin <matthew@kerwin.net.au> 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?