There is some code:
def func
def func
1
end
end
then I try the following in irb:
func
func.func
func
and get the result:
:func
1
1
Could anyone please explain what's going on? I kinda understand the first output but not the latter. Thanks!
You define a method inside a method in a global scope. Method definition returns a symbol with it's name.
func for the first time, it's redefined, by the inner func. That's why subsequent calls to func return 1.func.func. Try to define other method and you'll be able to call it on any symbol:def func
def func
1
end
end
def a
'a'
end
func.a
# 'a'
:asd.a
# 'a'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With