Kernel#loop calling using Fixnum object not happening?

Look at the below code :

irb(main):001:0> 2.class
=> Fixnum
irb(main):002:0> 2.class.included_modules
=> [Comparable, Kernel]
irb(main):003:0> 2.class.methods.grep /loop/
=> []
irb(main):004:0> 2.class.instance_methods.grep /loop/
=> []
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
irb(main):018:0> 2.loop
NoMethodError: private method `loop' called for 2:Fixnum
        from (irb):18
        from C:/Ruby193/bin/irb:12:in `<main>'
irb(main):019:0>

On the top level we can call `loop` method, but why not the same way
inside the method `show` ?

···

--
Posted via http://www.ruby-forum.com/.

Look at the below code :

irb(main):001:0> 2.class
=> Fixnum
irb(main):002:0> 2.class.included_modules
=> [Comparable, Kernel]
irb(main):003:0> 2.class.methods.grep /loop/
=>
irb(main):004:0> 2.class.instance_methods.grep /loop/
=>
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 `<main>'
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:

irb(main):001:0> method :loop
=> #<Method: Object(Kernel)#loop>
irb(main):002:0> Kernel.private_instance_methods(false).grep /loop/
=> [:loop]

Btw, there is Fixnum#times if you want to loop as many times as the
number indicates.

Kind regards

robert

···

On Thu, Jan 23, 2014 at 9:14 AM, Arup Rakshit <lists@ruby-forum.com> wrote:

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Robert Klemme wrote in post #1134107:

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

···

--
Posted via http://www.ruby-forum.com/\.

Robert Klemme wrote in post #1134107:

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.

Cheers

robert

···

On Thu, Jan 23, 2014 at 11:32 AM, Arup Rakshit <lists@ruby-forum.com> wrote:

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Robert Klemme wrote in post #1134138:

Robert Klemme wrote in post #1134107:

There is. Read your first posting again.

Ohh!! Sorry thanks.

Why do we need `Kernel#loop` without block version ?

Probably because of consistency with other iteration methods.

This point.. still I am searching.

···

On Thu, Jan 23, 2014 at 11:32 AM, Arup Rakshit <lists@ruby-forum.com> > wrote:

--
Posted via http://www.ruby-forum.com/\.