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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:13:00+00:00 2026-05-27T12:13:00+00:00

given module Foo def bar puts foobar end end I can do String.extend(Foo) and

  • 0

given

module Foo
  def bar
    puts "foobar"
  end
end

I can do

String.extend(Foo)

and as a consequence do

String.bar # => "foobar"

Why doesnt this work?:

a = String.new
a.bar # => NoMethodError: undefined method `bar' for "":String

Is it because ‘a’ is now and instance and .extend only works against class methods? Why does it lose the ‘new’ functionality I have given String via .extend?

  • 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-27T12:13:00+00:00Added an answer on May 27, 2026 at 12:13 pm

    Ruby allows you to add methods from a Module to a Class in two ways: extend and include.

    Using the module you gave:

    module Foo
      def bar
        puts "foobar"
      end
    end
    

    With extend the methods are added as class methods, and can be called directly on the class object itself:

    class Bar
      extend Foo
    end
    
    Bar.bar # => "foobar"
    

    Alternatively, you can call it outside of the class scope:

    class Bar
    end
    
    Bar.extend Foo
    
    Bar.bar # => "foobar"
    

    include is slightly different. It will add the methods as instance methods. They are only callable on an instance of the class:

    class Bar
      include Foo
    end
    
    Bar.bar # NoMethodError
    a = Bar.new
    a.bar # => "foobar"
    

    The key difference was that we first had to make an instance a, before we could call the instance method.

    As sepp2k noted, extend is can be called on any Object, not just Class objects. For example, you can go:

    class Bar
    end
    
    a = Bar.new
    a.extend Foo
    a.bar # => "foobar"
    

    However, bar will only be added to the single instance a. If we create a new instance, it you will not be able to call it.

    b = Bar.new
    b.bar # => MethodNotFoundError
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following module, module Foo def bar :baz end end def send_to_foo(method) #
Given a string like this: <a href=http://blah.com/foo/blah>This is the foo link</a> ... and a
Given a module with a singleton method like this: module Foo class << self
In Python, given a module X and a class Y, how can I iterate
Given a string with a module name, how do you import everything in the
given a list of module names (e.g. mymods = ['numpy', 'scipy', ...]) how can
Given a perl module Foo.pm with methods aSub() and bSub() my $obj = Foo->new();
I have a simple class that defines some constants, e.g.: module Foo class Bar
If I create a Node.js module augs that contains Object.foo = bar; Then type
Given URI /admin/article/index, why would this url mapping not work? /admin/$controller/$action?/$id?{ view = /admin/index

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.