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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:40:42+00:00 2026-06-05T16:40:42+00:00

I have written a module similar to the following: module One class Two def

  • 0

I have written a module similar to the following:

module One
  class Two
    def self.new(planet)
      @world = planet
    end
    def self.hello
      "Hello, #{@world}"
    end
  end
end

I was going to manipulate the module in the following way:

t = One::Two.new("World")
puts t.hello

Obviously, however, self.hello is not in t‘s scope. I realize that I could do the following:

t = One::Two
t.new("World")
puts t.hello

The previous method doesn’t feel right, so I am looking for an alternative.

  • 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-05T16:40:44+00:00Added an answer on June 5, 2026 at 4:40 pm
    module One
      class Two
        # use initialize, not self.new
        # the new method is defined for you, it creates your object
        # then it calls initialize to set the initial state.
        # If you want some initial state set, you define initialize.
        # 
        # By overriding self.new, One::Two.new was returning the planet,
        # not an initialized instance of Two.
        def initialize(planet)
          @world = planet
        end
    
        # because One::Two.new now gives us back an instance,
        # we can invoke it
        def hello
          "Hello, #{@world}"
        end
      end
    end
    
    t = One::Two.new 'World'
    t.hello # => "Hello, World"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a Python module, and I have two versions: a pure Python
I have written a class in python that implements __str__(self) but when I use
I have a large module written in JS which uses Canvas to draw and
I have an application consisting of different modules written in C++. One of the
i have written module which reads and write on /proc file and is working
I have written a DotNetNuke module for a customer that allows them to delete
I have written an Apache2 module in C, now I am at the point
I have written an IIS module (base IHttpModule) that does some custom (OpenAuth) authentication
I have written an F# module that has a list inside: module MyModule type
A friend and I have written an encryption module and we want to port

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.