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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T21:11:38+00:00 2026-05-21T21:11:38+00:00

I have the following Ruby script, in which class Foo includes module Baz, module

  • 0

I have the following Ruby script, in which class Foo includes module Baz, module Baz has a included hook to make Bar extended by the including class (Foo). I am just wondering why:

class << klass
  extend Bar #this doesn't work. Why?
end

does not work while:

klass.extend(Bar) works.

Here is my code:

#! /usr/bin/env ruby

module Baz
  def inst_method
    puts "dude"
  end 

  module Bar
    def cls_method
      puts "Yo"
    end
  end

  class << self
    def included klass
      # klass.extend(Bar) this works, but why the other approach below doesn't?
      class << klass
        extend Bar #this doesn't work. Why?
        def hello
          puts "hello"
        end
      end
    end
  end
end

class Foo
  include Baz
end

foo = Foo.new
foo.inst_method
Foo.hello
Foo.cls_method
  • 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-21T21:11:39+00:00Added an answer on May 21, 2026 at 9:11 pm

    Within the body of class << klass, self refers to the singleton class of klass, not klass itself, whereas in klass.extend(Bar), the receiver is klass itself. The difference comes from there.

    class A
    end
    
    class << A
      p self  # => #<Class:A>   # This is the singleton class of A, not A itself.
    end
    
    p A # => A     # This is A itself.
    

    And since you want to apply extend to klass, doing it within the body of class << klass does not work.

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

Sidebar

Related Questions

I have the following script which works with a 1 dimensional array. Is it
If I invoke an executable script: $ ./myscript which has the following defintion at
The Ruby API to Google SketchUp has a function, open_file , but I can't
I'm very new to Ruby, and currently running through the Ruby Koans to get
This is pretty weird. I have my public key added at host machine. I
I haven't been able to find a solution the following: After installing gems, such
I need to get the installation path for a given ruby gem and can't
For some reason the script below is not working. This is the code I
Recently I have become a fan of storing various settings used for my testing
So I'm new to rails and am actually following a video tutorial from Lynda.com

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.