irb(main):005:0> 2.class.private_instance_methods.grep /loop/
=> [:loop]
irb(main):013:0> class Fixnum
irb(main):014:1> def show
irb(main):015:2> loop
irb(main):016:2> end
irb(main):017:1> end
=> nil
That does not make much sense because loop wants a block.
irb(main):018:0> 2.loop
NoMethodError: private method loop' called for 2:Fixnum from (irb):18 from C:/Ruby193/bin/irb:12:in ’
irb(main):019:0>
On the top level we can call loop method, but why not the same way
inside the method show ?
You are not invoking it inside #show, you are invoking it directly.
And you cannot call it with a receiver because it is defined private
in Kernel:
On the top level we can call loop method, but why not the same way
inside the method show ?
You are not invoking it inside #show, you are invoking it directly.
And you cannot call it with a receiver because it is defined private
in Kernel:
I know #loop is a private method. And private method can’t have an
explicit receiver. But you can see in my #show method, there is no
explicit call. Why then error ?
There is. Read your first posting again.
Yes. I know Fixnum has #times method. But Kernel#loop gives us
Enumerator, without block. I am trying to understand what is the actual
applicable area for that design(without block).
There is not much. You can invoke #each on this (as well as #map
etc.) and it will run forever.
Why do we need Kernel#loop without block version ?
Probably because of consistency with other iteration methods.
On the top level we can call loop method, but why not the same way
inside the method show ?
You are not invoking it inside #show, you are invoking it directly.
And you cannot call it with a receiver because it is defined private
in Kernel:
Kind regards
robert
I know #loop is a private method. And private method can’t have an
explicit receiver. But you can see in my #show method, there is no
explicit call. Why then error ?
Yes. I know Fixnum has #times method. But Kernel#loop gives us
Enumerator, without block. I am trying to understand what is the actual
applicable area for that design(without block).
Why do we need Kernel#loop without block version ?
Thanks
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.