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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:39:24+00:00 2026-06-07T07:39:24+00:00

I just want to write a Ruby script (no rails) with a class that

  • 0

I just want to write a Ruby script (no rails) with a class that contains an array of ids. Here is my original class:

# define a "Person" class to represent the three expected columns
class Person <

  # a Person has a first name, last name, and city
  Struct.new(:first_name, :last_name, :city)

  # a method to print out a csv record for the current Person.
  # note that you can easily re-arrange columns here, if desired.
  # also note that this method compensates for blank fields.
  def print_csv_record
    last_name.length==0 ? printf(",") : printf("\"%s\",", last_name)
    first_name.length==0 ? printf(",") : printf("\"%s\",", first_name)
    city.length==0 ? printf("") : printf("\"%s\"", city)
    printf("\n")
  end
end

Now I would like to add an array called ids to the class, can I include it in the Struct.new statement like Struct.new(:first_name, :last_name, :city, :ids = Array.new) or create an instance array variable or any define separate methods or something else?

I would like to then be able to do things like:

p = Person.new
p.last_name = "Jim"
p.first_name = "Plucket"
p.city = "San Diego"

#now add things to the array in the object
p.ids.push("1")
p.ids.push("55")

and iterate over the array

p.ids.each do |i|
  puts i
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-06-07T07:39:27+00:00Added an answer on June 7, 2026 at 7:39 am
    # define a "Person" class to represent the three expected columns
    class Person
     attr_accessor :first_name,:last_name,:city ,:ids
    #  Struct.new(:first_name, :last_name, :city ,:ids) #used attr_accessor instead  can be used this too 
    
    def initialize
         self.ids = [] # on object creation initialize this to an array
    end
      # a method to print out a csv record for the current Person.
      # note that you can easily re-arrange columns here, if desired.
      # also note that this method compensates for blank fields.
      def print_csv_record
        print last_name.empty? ? "," : "\"#{last_name}\","
        print first_name.empty? ? "," : "\"#{first_name}\","
        print city.empty? ? "" : "\"#{city}\","
        p "\n"
      end
    end
    
    p = Person.new
    p.last_name = ""
    p.first_name = "Plucket"
    p.city = "San Diego"
    
    #now add things to the array in the object
    p.ids.push("1")
    p.ids.push("55")
    
    #iterate
    p.ids.each do |i|
      puts i
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

just think that when I opened my file then when I want to write
I am implementing in Ruby on Rails, and i want to do so, that
i just want to know, how to write Code behind(Hyperlink1_Click()), for the hyper link
I write a lot of Python code where I just want to read a
i just join phonegap, i want a built test application. i write some code
I want to write a little program that transforms my TeX files into HTML.
I'm a newbie to ruby, I want to know if I can use just
I am writing a simple ruby controller that just needs to respond as a
I'm writing a Ruby class that extends TCPSocket . Assume it looks something like
I want to write a simple Ruby DSL to translate some statements and expressions

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.