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

The Archive Base Latest Questions

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

class Hello @hello = hello def display puts @hello end end h = Hello.new

  • 0
class Hello
@hello = "hello"
    def display
        puts @hello
    end
end

h = Hello.new
h.display

I created the class above. It doesn’t print anything out. I thought the instance variable @hello was set during the class declaration. But when I call the display method the output is ‘nil’. What’s the correct way to do this?

  • 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-11T18:08:22+00:00Added an answer on May 11, 2026 at 6:08 pm

    Instance variables in ruby may be a bit confusing when first learning Ruby, especially if you are accustomed to another OO language like Java.

    You cannot simply declare an instance variable.

    One of the most important things to know about instance variables in ruby, apart from the notation with an @ sign prefix, is that they spring into life the first time they are assigned to.

    class Hello
      def create_some_state
        @hello = "hello"
      end
    end
    
    h = Hello.new
    p h.instance_variables 
    
    h.create_some_state
    p h.instance_variables
    
    # Output
    []
    ["@hello"]
    

    You can use the method Object#instance_variables to list all instance variables of an object.

    You normally “declare” and initialize all the instance variables in the initialize method. Another way to clearly document which instance variables that should be publicly available is to use the Module methods attr_accessor (read/write), attr_writer (write) and attr_reader (read). These methods will synthesize different accessor methods for the listed instance variable.

    class Hello
      attr_accessor :hello
    end
    
    h = Hello.new
    p h.instance_variables 
    
    h.hello = "hello"
    p h.instance_variables
    
    # Output
    []
    ["@hello"]
    

    The instance variable still isn’t created until it’s assigned to using the synthesized Hello#hello= method.

    Another important issue, like kch described, is that you need to be aware of the different contexts active when declaring a class. When declaring a class the default receiver (self) in the outermost scope will be the object that represents the class itself. Hence your code will first create a class instance variable when assigning to @hello on the class level.

    Inside methods self will be the object on which the method is invoked, hence you are trying to print the value of an instance variable with the name @hello in the object, which doesn’t exists (note that it’s perfectly legal to read a non existing instance variable).

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Those were not supported in the old days. And even… May 11, 2026 at 11:48 pm
  • Editorial Team
    Editorial Team added an answer What content-type is admin-ajax.php returning? When you use Fiddler or… May 11, 2026 at 11:48 pm
  • Editorial Team
    Editorial Team added an answer If you do :h E447 to look up the error… May 11, 2026 at 11:48 pm

Related Questions

Is it any easy way to use CherryPy as an web server that will
I have a service, as follows: The most basic (working) CherryPy 3.1 Windows service
In the example below, why do we say k.send :hello instead of k.receive :hello
(Title was: How to write a unit test for a DBUS service written in

Trending Tags

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

Top Members

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.