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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:59:56+00:00 2026-06-16T03:59:56+00:00

So I want a module with a variable and access methods. My code looks

  • 0

So I want a module with a variable and access methods.
My code looks something like this

module Certificates
  module Defaults

  class << self
    attr_accessor :address

    def get_defaults
      address = "something"
      make_root_cert
    end

    def make_root_cert
      blub = address
      # do somthing
    end
  end
end

I inspected it with pry.
The result is

  • Certificates::Defaults has methods called address and address=.
  • If I call address in the get_defaults method it returns “something” as expected
  • If I call it in make_root_cert it returns nil

I used this way of attr_accessor creation in another module and it worked fine. I hope I’m just misunderstanding the way ruby works and somebody can explain why this example doesn’t work. Maybe using the implementation details of the ruby object model.

Jeremy is right.

My findings

This seems inconsistent to me.

  • If you use the expression “address” and the instance variable has not been set it returns the local variable
  • If the instance variable has been set and the local variable not it returns the instance variable.
  • If both have been set it returns the local variable.

On the other hand address=”test” always sets the local variable.

  • 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-16T03:59:57+00:00Added an answer on June 16, 2026 at 3:59 am

    In your get_defaults methods, address is a local variable. To use the setter, you have to type this:

    self.address = "something"
    

    That will properly call the address= method.

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

Sidebar

Related Questions

I'm writing a test for some existing Ruby code which looks something like module
Currently my product page URL is like http://www.localhost:80/products/default.aspx?code=productCode I want to access product page
Possible Duplicate: Javascript outer scope variable access I have a javascript module that looks
I want to ask something about the new module 'Domain' in node v0.8. var
I want to create a multi-module (maven) spring MVC application, with modules like: web
Possible Duplicate: How to programmatically set a global (module) variable? I have a class
I am trying to save a global variable in my module and access it
I want the following module to be included in a class I have: module
I want to use a Dictionary as a private variable in a module. I
I want create module which update list of usb devices automatically (not only mass

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.