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 4617524
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:08:11+00:00 2026-05-22T02:08:11+00:00

Just for fun, again, but is it possible to take a block that contains

  • 0

Just for fun, again, but is it possible to take a block that contains method definitions and add those to an object, somehow? The following doesn’t work (I never expected it to), but just so you get the idea of what I’m playing around with.

I do know that I can reopen a class with class << existing_object and add methods that way, but is there a way for code to pass that information in a block?

I guess I’m trying to borrow a little Java thinking here.

def new(cls)
  obj = cls.new
  class << obj
    yield
  end
  obj
end

class Cat
  def meow
    puts "Meow"
  end
end

cat = new(Cat) {
  def purr
    puts "Prrrr..."
  end
}

cat.meow
  # => Meow

# Not working
cat.purr
  # => Prrrr...

EDIT | Here’s the working version of the above, based on edgerunner’s answer:

def new(cls, &block)
  obj = cls.new
  obj.instance_eval(&block)
  obj
end

class Cat
  def meow
    puts "Meow"
  end
end

cat = new(Cat) {
  def purr
    puts "Prrrr..."
  end
}

cat.meow
  # => Meow

cat.purr
  # => Prrrr...
  • 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-22T02:08:11+00:00Added an answer on May 22, 2026 at 2:08 am

    You can use class_eval(also aliased as module_eval) or instance_eval to evaluate a block in the context of a class/module or an object instance respectively.

    class Cat
      def meow
        puts "Meow"
      end
    end
    
    Cat.module_eval do
      def purr
        puts "Purr"
      end
    end
    
    kitty = Cat.new
    kitty.meow #=> Meow
    kitty.purr #=> Purr
    
    kitty.instance_eval do
      def purr
        puts "Purrrrrrrrrr!"
      end
    end
    
    kitty.purr #=> Purrrrrrrrrr!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just for fun I created a project that created about 5 GB of memory
I'm writing some code (just for fun so far) in Python that will store
Sometimes I want to build Python or GCC from scratch just for fun, but
Just for fun I would like to count the conditional probabilities that a word
Just for fun, I implemented my own stack, but, without using a linked list,
I am building a website just for fun of learning programming more fully, but
Possible Duplicate: Can you write object oriented code in C? Hi! Just for the
Just for fun, how close can we get to debug an application in C#
I built (just for fun) 3 classes to help me log some events in
I'm learning about AI and (just for fun and practice, not profit or anything

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.