Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7056431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:51:59+00:00 2026-05-28T03:51:59+00:00

Can someone explain why the last yielder throws a no block given? class Foo

  • 0

Can someone explain why the last yielder throws a no block given?

class Foo
  def yielder
    yield "hello"
  end
end

class Mod

  def initialize
    @@foo = Foo.new
  end

  def self.foo
    @@foo
  end

end

worker = Mod.new

Mod.foo.yielder do |hello|
  puts hello
end

Mod.foo.class.send(:define_method,:yielder) do
   yield "new hello"
end

Mod.foo.yielder do |hello|
  puts hello
end

Gives:

hello
test.rb:27:in `block in ‘: no block given (yield) (LocalJumpError)
from test.rb:30:in `’

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:51:59+00:00Added an answer on May 28, 2026 at 3:51 am

    A short introduction:

    You don’t need the Mod-instance, if you define @@foo outside initialize.

    You don’t need the Mod class to get the problem:

    class Foo
      def yielder
        p 2
        yield "hello"
      end
    end
    foo = Foo.new
    
    foo.yielder do |hello|
      puts hello
    end
    
    foo.class.send(:define_method,:yielder) do
      p 1
       yield "new hello"
    end
    
    foo.yielder do |hello|
      puts hello
    end
    

    You may shorten your example again:

    class Foo
    end
    foo = Foo.new
    
    foo.class.send(:define_method,:yielder) do
       yield "new hello"
    end
    
    foo.yielder do |hello|
      puts hello
    end
    

    This is the same as:

    class Foo
      define_method(:yielder) do
        yield "new hello"
      end
    end
    
    foo = Foo.new
    foo.yielder do |hello|
      puts hello
    end
    

    End of Introduction.

    And now, I’m not sure if I understood correct what you want (and if I understand ruby correct 😉 )
    define_method accepts a block and use it as method body.
    If the new method should receive a block on its own, you must define it in the interface of the definition and call it:

    class Foo
      define_method(:yielder) do | &prc |
        prc.call("new hello")
      end
    end
    
    foo = Foo.new
    foo.yielder do |hello|
      puts hello
    end
    

    Or the same logic in your example:

    class Foo
      def yielder
        yield "hello"
      end
    end
    
    class Mod
    
      def initialize
        @@foo = Foo.new
      end
    
      def self.foo
        @@foo
      end
    
    end
    
    worker = Mod.new
    
    Mod.foo.yielder do |hello|
      puts hello
    end
    
    Mod.foo.class.send(:define_method,:yielder) do | &prc |
       prc.call "new hello"
    end
    
    Mod.foo.yielder do |hello|
      puts hello
    end
    

    To make the code more robust, I would recommend some checks with block_given?.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone explain the following behaviour: class derivedset1(frozenset): def __new__(cls,*args): return frozenset.__new__(cls,args) class derivedset2(set):
Can someone explain the difference between .last() and :last ? I can't seem to
Looking at the last demo for the .change() jQuery function. Can someone explain to
I dont really understand what the last while loop is doing, can someone explain
Can someone explain why the last assignment in the code below is not valid
Can someone explain why the last line prints out -1? It happens when copy
Can someone explain the output of the following code? #include <iostream> template <class T>
Can someone please explain why I'm getting this error? My code: def x(n): if
Can someone explain the mechanics of a jump table and why is would be
Can someone explain to me the advantages of using an IOC container over simply

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.