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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:02:35+00:00 2026-05-14T19:02:35+00:00

def singleton_class class << self self end end class Human proc = lambda {

  • 0
    def singleton_class
       class << self
         self
       end
    end

    class Human
      proc = lambda { puts 'proc says my class is ' + self.name.to_s }

      singleton_class.instance_eval do
        define_method(:lab)  do 
          proc.call
        end
      end
    end

    class Developer < Human
    end

    Human.lab # class is Human
    Developer.lab # class is Human ; oops

Following solution works.

def singleton_class
  class << self
    self
  end
end

class Human
  proc =  lambda { puts 'proc says my class is ' + self.name.to_s }
  singleton_class.instance_eval do
    define_method(:lab) do
      self.instance_eval &proc
    end
  end
end

class Developer < Human
end

Human.lab # class is Human
Developer.lab # class is Human ; oops

Why Developer.lab is reporting that it is Human ? And what can be done so that proc reports Developer when Developer.lab is invoked.

  • 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-14T19:02:35+00:00Added an answer on May 14, 2026 at 7:02 pm

    It’s subtle, but it boils down to simply calling the block (in which case it acts as a normal closure, and self corresponds to where it was defined, i.e. in Human), or using it (directly) as a block for a method definition or instance_eval:

    def singleton_class
       class << self
         self
       end
    end
    
    
    class Human
    
      PROC = proc { puts 'proc says my class is ' + self.name.to_s }
    
      singleton_class.instance_eval do
        define_method(:lab)  do 
          PROC.call
        end
        define_method(:lab2, &PROC.method(:call))
    
        define_method(:lab3)  do 
          instance_eval(&PROC)
        end
        define_method(:lab4, &PROC) 
      end
    end
    
    class Developer < Human
    end
    
    Human::PROC.call  # => "class is Human"  (original closure)
    Developer.lab     # Same as previous line, so "class is Human"  (original closure)
    Developer.lab2    # ditto
    
    Developer.instance_eval(&Human::PROC)  # => "class is Developer"  (instance_eval changes sets a different scope)
    Developer.lab3    # Same as previous line, so "class is Developer"
    Developer.lab4    # ditto
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 513k
  • Answers 513k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I can be wrong, but maybe something like this: location… May 16, 2026 at 5:58 pm
  • Editorial Team
    Editorial Team added an answer Since you are new to game dev, I would not… May 16, 2026 at 5:58 pm
  • Editorial Team
    Editorial Team added an answer Select your Project in Solution Explorer. Right click it and… May 16, 2026 at 5:58 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

class Product < ActiveRecord::Base set_table_name 'produce' end module ActiveRecord class Base def self.set_table_name name
Following code works def lab Proc.new { return foo1 }.call return foo2 end puts
module MyModule def my_method; 'hello'; end end class MyClass class << self include MyModule
How can I make this code work? class Meta @array = [:a,:b] def self.method_missing(name,
class Article(models.Model): def user_may_see_full_version(self, user): # do something very sophisticated with the user return
In Ruby def my_func(foo,bar,*zim) [foo, bar, zim].collect(&:inspect) end puts my_func(1,2,3,4,5) # 1 # 2
Let's say I have a singleton class like this: class Settings include Singleton def
def show render :text => params.inspect end What is render :text => ? What
I have this method: def is_active(self): if self.is_current_node(): return True cur_children = self.get_children() while
I'm trying to write a DSL that allows me to do Policy.name do author

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.