1

I am getting below error when executing code:

example.rb:9:in `<main>': undefined method `each' for main:Object (NoMethodError)

Line 9 is second last line in my code.

My code:

class TargetProvider
  def each(target,&block)
    block.call(target)
  end   
end

tp = TargetProvider.new
each { puts "Hello!" }
tp.each(1, each)

My motive is to call block with target parameter.

How can I pass a block outside class. Any help please.

Sebastián Palma
  • 32,692
  • 6
  • 40
  • 59
ojas
  • 2,150
  • 5
  • 22
  • 37

2 Answers2

1

Here is yout code:

class TargetProvider
  def each(target,&block)
    block.call(target)
  end   
end

tp = TargetProvider.new
each { puts "Hello!" } # this line is trying to run a method called each
tp.each(1, each)

You need to define a lambda or a proc and store it in a variable, or use the implicit block of the each. Here is an example using a lambda:

class TargetProvider
  def each(target,&block)
    block.call(target)
  end   
end

tp = TargetProvider.new
each_block = lambda { |a| puts "Hello!" }
tp.each(1, &each_block)

Here is an example using the implicit block:

class TargetProvider
  def each(target,&block)
    block.call(target)
  end   
end

tp = TargetProvider.new
each_block = lambda { |a| puts "Hello!" }
tp.each(1) { puts "Hello!" }
Fede
  • 460
  • 3
  • 10
0

Well, the problem is here:

tp = TargetProvider.new
each { puts "Hello!" } # Here
tp.each(1, each)

Here, you are calling each method, not declaring a new variable. In Ruby, if you call a method without specifying its object (via object.method), interpreter searches for that method in MainObject.

It's clear that there no such method in it, so that's what an error is about.

What you may want to achieve is looks like this:

tp = TargetProvider.new
tp.each(1) { puts "Hello!" }

That code calls TargetProvider's each method passing it a block.

Egor Biriukov
  • 649
  • 1
  • 7
  • 16
  • "In Ruby, if you call a method without specifying its object (via `object.method`), interpreter searches for that method in `MainObject`." – No. The implicit receiver is `self`, not `MainObject` (whatever that is.) – Jörg W Mittag Jun 18 '17 at 00:41
  • @JörgWMittag And what is `self` outside any class, in top-level context? Right it's `main` object, check it out in irb. – Egor Biriukov Jun 19 '17 at 08:07