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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:20:27+00:00 2026-06-03T19:20:27+00:00

I am writing a class in Ruby where I have instance variables (i.e. @person_summary_info

  • 0

I am writing a class in Ruby where I have instance variables (i.e. @person_summary_info, @name, @dob, @favorite_food) for the class.

To parse a piece of text, I have a public method that I call from outside the class (let’s call it interpret).

This method calls some private class methods such as get_name that use @person_summary_info to extract the respective piece of information (in this case, the name of the person). Should those private methods:

a) use the instance @person_summary_info, or get that information through a parameter passed to them (i.e. get_name vs get_name(person_summary_info))

b) modify the instance variable directly and return nothing, or modify nothing outside the scope of the function, and return the result (i.e. inside get_name, set @name = 'John', or return 'John')?

What is the best practice here?
Thanks!

  • 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-03T19:20:30+00:00Added an answer on June 3, 2026 at 7:20 pm

    I have included my best representation of your question in code at the bottom of my answer, but I’d like to present my solution as I understand your dilemma first…

    Do this if your name attribute is meant to be publicly accessible:

    class Person
      attr_accessor :name
    
      def initialize(name)
        @name = name
      end
    
      def interpret(text_to_parse)
        # I have no idea what you are parsing in real life
        self.name = text_to_parse.split.last
      end
    end
    
    person = Person.new("Frederick")
    puts person.name
    # => "Frederick"
    person.interpret("Please, call me Fred")
    puts person.name
    # => "Fred"
    

    Do this if your name attribute should not be (easily) publicly accessible: (For what it’s worth, pretty much anything can be accessed one way or another in Ruby. One of the many things that make it awesome!)

    class Person
      def initialize(name)
        @name = name
      end
    
      def interpret(text_to_parse)
        # I have no idea what you are parsing in real life
        @name = text_to_parse.split.last
      end
    end
    
    person = Person.new("Frederick")
    puts person.instance_variable_get("@name")
    # => "Frederick"
    person.interpret("Please, call me Fred")
    puts person.instance_variable_get("@name")
    # => "Fred"
    

    And, as mentioned above, here’s my best translation of your question into code:

    class Person
      def initialize
        @person_summary_info = { name: "foo" }
        @name = "bar"
        @dob = "baz"
        @favorite_food = "beer"
      end
    
      def interpret(text_to_parse)
        # Some kind of parsing?
        get_name_1
        # OR
        get_name_2(@person_summary_info)
        # OR
        get_name_3
        # OR
        @name = get_name_4
      end
    
      private
      def get_name_1
        @person_summary_info[:name]
      end
    
      def get_name_2(person_summary_info)
        person_summary_info[:name]
      end
    
      def get_name_3
        @name = 'John'
      end
    
      def get_name_4
        'John'
      end
    end
    

    Hopefully, you can see why there’s some confusion in the comments about what you are asking exactly. If nothing else, maybe seeing this will help you to form your question more clearly so we can help!

    Finally, you should avoid writing your own getters/setters in Ruby unless you need to hook in some custom code to the getting/setting processes — use the class-level attr_reader/attr_writer/attr_accessor macros to create them for you.

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

Sidebar

Related Questions

I'm writing a ruby-on-rails library module: module Facets class Facet attr_accessor :name, :display_name, :category,
I've been learning Ruby for a class and have been writing a sample game.
Scala programmer should have known that this sort of writing : class Person{ var
I'm writing a Ruby class that extends TCPSocket . Assume it looks something like
I'm writing a simple command-line board game in Ruby. I need to have main
I have a simple model setup in my Ruby on Rails app. (User {name,
I am writing an extension method for the String class to clean up non-ASCII
I am writing ruby on rails application, which will have 2 different users types
I'm writing a small program in Ruby to parse a hand history log from
I'm working on learning TDD while writing some small ruby programs. I have the

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.