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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:14:31+00:00 2026-05-29T20:14:31+00:00

Two problems, that probably are related: I’m retreiving a number of ‘persons’ from a

  • 0

Two problems, that probably are related:

I’m retreiving a number of ‘persons’ from a YAML-file to an array, and now i’m trying to create classes from that array.

These objects are then to placed in a new array.

It actually works out fine, if you dont consider the fact that the object added last replaces all the previously added.

In my case i get five identical copies of object #5, where i rather like to see five different ones.

My guess is that the error results somewhere in my iterator to get all the ‘persons’ from the YAML.

I’m getting a cuople of warnings, regarding the ‘re-use’ of constants:
NEWSTR and NEWAL.

getPost = 0
loopa = 0

     while loopa < personsInYAML
        NEWSTR = TEST.fetch(getPost)
        NEWAL = NEWSTR.split(' ')
        getPost+=1

        puts "*****************************************"
        nyloop = loopa+1
        puts "PERSON: " + nyloop.to_s + " name: " + NEWAL.fetch(1)

        nameToArray = Person.new
        outputArray.insert(loopa, nameToArray)
        loopa+=1
    end

Persons-class

class Person

    def initialize
        @name
        @age
        @length
        @weight
        @misc
    end


    def name
        name = NEWAL.fetch(1)
        return name
    end

    if NEWAL.include?("age:")

        def age
            x = NEWAL.index("age:")+1   
            age =  NEWAL.fetch(x)
            return age
        end
    end

    if NEWAL.include?("length:")
        def length
            x = NEWAL.index("length:")+1
            length = NEWAL.fetch(x)
            return length
        end
    end

    if NEWAL.include?("weight:")
        def weight
            x = NEWAL.index("weight:")+1
            weight = NEWAL.fetch(x)
            return weight
        end
    end

    if NEWAL.include?("misc:")
        def misc
            x = NEWAL.index("misc:")+1
            misc = NEWAL.fetch(x)
            return misc
        end
    end
end
  • 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-29T20:14:32+00:00Added an answer on May 29, 2026 at 8:14 pm

    You’re taking the wrong approach to populating your Person class. The only thing your loop is doing is to create brand new Person classes and stick them in an array. It isn’t actually initializing the person class at all.

    It looks like what you are trying to do is to use a constant (which you don’t hold constant) to pass information to the Person class. However, the code that you have in your Person class that is outside of the methods is only going to be run once – when the class loads for the first time, NOT at the time that you make a new Person.

    You’d be better off changing your initialize method to take some arguments, and to create the class with appropriate arguments within the loop.

    def initialize(name, age = nil, length = nil, weight = nil, misc = nil)
      # assign instance variables here
      @name = name
      ...
    end
    

    You appear to be trying to create dynamic accessors to the instance variables. This doesn’t make a whole lot of sense. Just define accessors on all of them, and handle the case where the instance variables are nil in whatever code is calling the Person class.

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

Sidebar

Related Questions

I have some localization problems in my webpage. There are basically two problems (that
I have my program that can draw rectangles. I have two problems I can't
I spent around two hours on that problem, and I have visited these stackoverflow
I have two pairs of drag/drop/sortable lists using JqueryUI.http://jqueryui.com/demos/sortable/ My problem is that I
I need to associate two models with a simple has_many. The problem is that
I like select the two inputbox after the automplete box. The problem is that
I am using DllImport in my solution. My problem is that I have two
Problem I have computed a probability density function that depends on two variables. I
Here is the problem: I have two columns in a table that, for each
I have a problem with CR (version that ships with VS2008). Report has two

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.