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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:36:48+00:00 2026-05-12T17:36:48+00:00

Assume, i got the following file ( input.txt ): name = Peter age =

  • 0

Assume, i got the following file (input.txt):

name = "Peter"
age = 26
family_status = married

Mentioned lines can be stored in a random order, lie:

family_status = married
name = "Peter"
age = 26

In my program I also have variables family_status, age and name. How do I in a single cycle read those lines from file and assign correspond variables with the values?

  • 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-12T17:36:48+00:00Added an answer on May 12, 2026 at 5:36 pm

    Setting Variables

    This depends on several facts.

    • What kind of variables do you have (local variables, instance variables, class variables, or global variables)
    • What kind of type is family_status (String, Symbol, whatever)

    I assume you are using instance variables for this:

    def read_vars(io, vars)
      io.each do |line|
        # ensure the line has the right format (name = var)
        raise "wrong format" unless line=~ /^\s*(\w+)\s*=\s*"?(.*?)"?\s+$/
        var= :"@#{$1}"
        # pick the type of the data
        value= case vars[var]
        when String
          $2
        when Integer
          $2.to_i
        when Symbol
          $2.to_sym
        else
          raise "invalid var"
        end
        instance_variable_set(var, value)
      end
    end
    
    read_vars(File.read("input.txt", :@age => Integer, :@name => String, :@family_status => Symbol )
    

    If you are not using instance variables you have to change the instacne_variable_set and var= :"@... line to you needs. This code has the following advantages:

    • You control which variables can be set
    • You control which types these variables have
    • You can easily add new variables and/or change types of variables without changing the read code
    • You can use this code to read entirely different files, without writing new code for it

    reading as YAML

    If your needs are not as specific as in your question I would go an entirely different approach to this.

    I would write the input.txt as a yaml file. In yaml syntax it would look like this:

    ---
    name: Peter
    age: 26
    family_status: :married
    

    You can read it with:

    YAML.load(File.read("input.txt")) # => {"name" => "Peter", "age" => 26, "family_status" => :married }
    

    Be carefull if you don’t control the input.txt file, you don’t control which types the data will have. I would name the file input.yaml instead of input.txt. If you want to know more, about how to write yaml files have a look at: http://yaml.kwiki.org/?YamlInFiveMinutes. More infos about yaml and ruby can be found at http://www.ruby-doc.org/stdlib/libdoc/yaml/rdoc/index.html.

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

Sidebar

Related Questions

Background: Let's assume I've got the following class: class Wrapped<T> : IDisposable { public
Let's assume we've got the following Java code: public class Maintainer { private Map<Enum,
I have got the following simplified schema: CREATE TABLE [file] ( id UNIQUEIDENTIFIER NOT
Let's assume that we've got 2 windows processes , Process A is the sender,
I've got a process something like a workflow, assume it like this: Prepare Eat
Assume I have the following string: Hellotoevryone<img height=115 width=150 alt= src=/Content/Edt/image/b4976875-8dfb-444c-8b32-cc b47b2d81e0.jpg />Iamsogladtoseeall. This
Assume I have access to a SMB server at IP 1.2.3.4, how can I
I got a LDAP schema but ldifde wraps long lines so after googled I
I use the following program to rename a directory, but I got the exception,
I doubt anyone can help with this question because of the following in Erlang's

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.