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

The Archive Base Latest Questions

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

In Ruby, I have this class: class Position attr_reader :x, :y def initialize(x, y)

  • 0

In Ruby, I have this class:


class Position
  attr_reader :x, :y
  def initialize(x, y)
      @x, @y = x, y
  end
end


What I want to do is to access x and y variables using the symbol, something like this:

axis = :x
pos = Position.new(5,6)
 #one way:
pos.axis # 5 (pos.x)
 #other way:
pos.get(axis) # 5 (pos.x)

Thanks to this question I've found with this code, I can achieve the second behavior.

#...
class Position
  def get(var)
    instance_variable_get(("@#{var}").intern)
  end
end


But it seems ugly and inefficient (especially converting symbol to string and back to symbol). Is there a better way?

  • 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-20T22:06:38+00:00Added an answer on May 20, 2026 at 10:06 pm

    Here are ways to do both techniques. Assuming we already have your class definition,

    position = Position.new(1, 2)
    axis = :x
    position.send axis #=> 1
    axis = :y
    position.send axis #=> 2
    

    The Object#send method accepts at least a symbol representing the name of the method to call, and call it. You can also pass arguments to the method after the name and a block, too.

    The second way to do this (using your Position#get method) is

    class Position
      def get(axis)
        send axis
      end
    end
    
    position = Position.new(1, 2)
    axis = :x
    position.get axis #=> 1
    axis = :y
    position.get axis #=> 2
    

    I recommend this way because it encapsulates the technique for getting the values. Should you need to change it later, you don’t need to change all the code that uses Position.

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

Sidebar

Related Questions

I have ruby code like this: module Hello class Hi def initialize() puts self.module.name
In Ruby, when I do something like this: class Foo ... def initialize( var
Let's say I have this code snippet running. class Song def initialize(name, artist, duration)
Say I have this Ruby code in test.rb module MyModule class TestClassA end class
I have something like this: class Person < ActiveRecord::Base has_many :things do def [](kind)
I am using the Mongo Ruby driver and have this block of Ruby code
I have a ruby application that executes ant as a subprocess using backtick. This
I have a Ruby class called LibraryItem . I want to associate with every
Suppose I have a class in Ruby: class Test def method(arg1, arg2) return arg1+arg2
I'm using Rails 2.3.4/Ruby 1.9.1 for my application. In the view I have this

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.