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

  • Home
  • SEARCH
  • 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 9196423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:50:47+00:00 2026-06-17T21:50:47+00:00

I want to create a class method that takes a block of method definitions

  • 0

I want to create a class method that takes a block of method definitions and injects it into the class. Now, self is indeed the Tom object so class << self does open it up, but yield doesn’t seem to work. My theoretical knowledge is not that deep, so I am not sure why this is not working. I might be getting this totally wrong, so feel free to discuss alternatives.

class Tom < Person
  mega_methods do
    def hiya!
      puts 'hiYA!'
    end
  end
end

class Person
  def self.mega_methods
    ...
    class << self
      yield
    end
  end
end

Tom.hiya!

I am aware I could just define the methods in Tom using class << self or that I could include class << self in the block.

I also figured out this alternative:

def self.mega_methods &block
  if block_given?
    extension =  Module.new(&Proc.new)
    self.extend(extension)
  end
end

This question is more about helping me understand the workings of Ruby rather than solving a specific issue.

  • 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-06-17T21:50:48+00:00Added an answer on June 17, 2026 at 9:50 pm

    It’s a little bit weird, but OK if you are exploring Ruby.

    class Person
      puts "evaluating Person's body ..."
      def self.mega_methods
          puts 'in Person#self.mega_methods, about to yield ...'
          yield
      end
    end
    
    class Tom < Person
      puts "evaluating Tom's body ..."
      print 'Tom.singleton_methods : '; p Tom.singleton_methods
      mega_methods do
        puts 'in Tom, about to define self.hiya!'
        def self.hiya!
          puts 'hiYA!'
        end
      end
      print 'Tom.singleton_methods : '; p Tom.singleton_methods
    end
    
    Tom.hiya!
    

    Execution :

    $ ruby -w t.rb 
    evaluating Person's body ...
    evaluating Tom's body ...
    Tom.singleton_methods : ["mega_methods"]
    in Person#self.mega_methods, about to yield ...
    in Tom, about to define self.hiya!
    Tom.singleton_methods : ["mega_methods", "hiya!"]
    hiYA!
    

    Note that the exclamation point in the name of a method is, by convention, reserved for destroying methods, such as String#sub! which modifies the receiver string.

    See also What does def `self.function` name mean?

    It’s all about self !

    class Person2
      def self.mega_methods
        print 'in Person2 self='; p self
        class << self
          print 'in class << self self='; p self
          yield
        end
      end
    end
    
    class Tom2 < Person2
      mega_methods do
        puts "in Tom2, about to define self.hiya! for self=#{self}"
        def self.hiya!
          puts 'hiYA!'
        end
        def hi
          puts 'hi'
        end
      end
    end
    
    print 'Tom2.singleton_methods : '; p Tom2.singleton_methods
    print 'Tom2.instance_methods  : '; p Tom2.instance_methods(false)
    Tom2.hiya!
    Tom2.new.hi
    

    Execution :

    $ ruby -w t2.rb 
    in Person2 self=Tom2
    in class << self self=#<Class:Tom2>
    in Tom2, about to define self.hiya! for self=Tom2
    Tom2.singleton_methods : ["mega_methods", "hiya!"]
    Tom2.instance_methods  : ["hi"]
    hiYA!
    hi
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a Javascript class/object that allow me to have various method:
I want to create a substr method in C++ in a string class that
I want to create a custom class that takes a color as one of
I want to dynamically create instance method of child class through class method of
I want to create a generic method to serizlize a class to text (for
I want to create a class that initializes a timer which will be used
I want to create whois class like that public class DomainInfo { public string
All, I want to create a function that takes a symbol representing a java
I want to create a method which takes a string argument and returns a
If I have a method that takes any object as a parameter I will

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.