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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T15:20:06+00:00 2026-05-21T15:20:06+00:00

In Ruby, is there a way to do something like class Foo attr_reader :var_name

  • 0

In Ruby, is there a way to do something like

class Foo
    attr_reader :var_name :reader_name #This won't work, of course
    def initialize
        @var_name = 0
    end
end

# stuff here ....

def my_method
    foo = Foo.new
    assert_equal 0,foo.reader_name
end

In other words, is there a standard way to make an accessor method for a variable that uses a different name than the variable. (Besides hand-coding it, of course.)

  • 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-21T15:20:06+00:00Added an answer on May 21, 2026 at 3:20 pm

    You could use alias_method:

    class Foo
      attr_reader :var_name
      alias_method :reader_name, :var_name
      def initialize
        @var_name = 0
      end
    end
    

    The var_name method built by attr_reader would still be available though. You could use remove_method to get rid of the var_name method if you really wanted to (but make sure you get everything in the right order):

    class Foo
      attr_reader :var_name
      alias_method :reader_name, :var_name
      remove_method :var_name
      def initialize
        @var_name = 0
      end
    end
    

    If you really wanted to, you could monkey patch an attr_reader_as method into Module:

    class Module
      def attr_reader_as(attr_name, alias_name)
        attr_reader attr_name
        alias_method alias_name, attr_name
        remove_method attr_name
      end
    end
    
    class Foo
      attr_reader_as :var_name, :reader_name
      def initialize
        @var_name = 0
      end
    end
    

    A better attr_reader_as would take a hash (e.g. { var_name: :reader_name, var_name: :reader_name2}) but I’ll leave that as an exercise for the reader.

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

Sidebar

Related Questions

I'd like to do something like this in ruby: safe_variable = begin potentially_nil_variable.foo rescue
In Ruby, I have this class: class Position attr_reader :x, :y def initialize(x, y)
In ruby, if I do require foo, is there a way to subsequently determine
I would like to do something like this from a Ruby script, within a
In Ruby, can I do something C-like, like this (with my made-up operator '&'):
We have a model association that looks something like this: class Example < ActiveRecord::Base
Is there a way to iterate over a Time range in Ruby, and set
Is there a way to access everything in the symbol table in Ruby? I
In ruby, is there a way to undefine a variable or constant once it's
Is there an easy way to tell if a ruby script is already running

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.