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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:51:44+00:00 2026-05-15T16:51:44+00:00

I have the following ruby code: class Mp def initialize Test.new.mytest Work.new.mywork ha address

  • 0

I have the following ruby code:

class Mp
  def initialize

    Test.new.mytest
    Work.new.mywork
    ha
    address

  end

  def ha
    puts "message from ha"
  end

  def address

   a='see'

  end

end


class Test

  def mytest
    m=Mp.new
    puts "Message from test do you #{m.address}"
  end
end

class Work

  def mywork
    puts "message from work"
  end

end

Mp.new

This works fine except the part in def mytest where I’m trying to put out the m.address. Thanks for your help in advance.

  • 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-15T16:51:44+00:00Added an answer on May 15, 2026 at 4:51 pm

    Actually the reason it doesn’t work has nothing to do with printing the address. It’s one line before that:

    m = Mp.new this creates a new Mp object. However inside Mp‘s initialize method a new Test object is created and its mytest method is called. The mytest method then again creates a new Mp object and so on. In other words: Test#mytest and Mp#initialize are mutually and infinitely recursive.

    Edit in response to your comment:

    I’m not quite sure I understood the question. If you mean “How do I access the variable a which was set in the address method, after address has been called”: you don’t. a is a local variable that goes out of scope once the method has returned. If you want to set an instance variable use @a = 'see'. @ denotes instance variables in ruby. If you want to be able to access that variable from outside the object, use attr_accessor :a to define accessor methods for @a.

    An example:

    class Mp
      attr_accessor :c
    
      def initialize
        initialize_variables
        puts @c
        puts @b # I can access @c and @b here because it's an instance variable
                # and I'm within the same object
    
        # puts a # This does not work because a is a local variable from the
                 # initialize_variables method and no longer in scope
      end
    
      def initialize_variables
        a = "a"
        @b = "b"
        @c = "c"
        puts a  # I can access a here because I'm still inside the method
                # where a was defined
      end
    end
    
    m = Mp.new
    # puts m.a
    # puts m.b  # These don't work because there are no methods a or b
    
    puts m.c  # This works because attr_accessor defined a method c which
              # returns the content of m's @c variable
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 508k
  • Answers 508k
  • 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 Since toDate (which can be null) is a host variable,… May 16, 2026 at 4:16 pm
  • Editorial Team
    Editorial Team added an answer This is a long shot, but it could be that… May 16, 2026 at 4:16 pm
  • Editorial Team
    Editorial Team added an answer Instead of using the ViewDataDictionary can't you simply add the… May 16, 2026 at 4:16 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

I'm playing around with Ruby and I have written the following code: module IdAndNameRedefine
In order to implement auto-vivification of Ruby hash, one can employ the following class
I have some trivial markup that looks like the following: <li class=someclass> <=% t'model.attr'
I have created a delegated Array class like this: class LeadPartArray < DelegateClass(Array) def
It is said that when we have a class Point and knows how to
I'm attempting to design an achievement system in Ruby on Rails and have run
I am in the process of implementing a Java library in Ruby. I have
I am trying to use IO.popen in order to put (with .puts method) and
I am trying to setup a simple UDP client and server using Ruby. The
I'm learning Ruby (using version 1.8.6) on Windows 7. When I try to run

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.