please help load from txt

Discussion in 'Ruby' started by Lark Work, Nov 15, 2010.

  1. Lark Work

    Lark Work Guest

    hi i new to this forum and i have a problem a made a script containing a
    load data from a file but i want it to load one line at the time the
    data from that line must be used but i don't know how i (and maybe if
    its possible i want to call a line with gets.chomp of something) so can
    u please help me this is my code :

    class Class_load
    def initialize(textfile)
    @text = IO.readlines(textfile)
    @data = []
    for i in
    @text.each(',') {|s| @data.push(s.delete(","))}
    end
    show_load_data
    end
    def load_data
    end
    def show_load_data
    puts "Name: #{@data[0]} #{@data[1]}"
    puts "Callname: #{@data[2]}"
    puts "Age: #{@data[3]}"
    end
    end

    Class_load.new("data.txt")


    and this is how my data.txt looks like:

    name,lastname,callname,age
    name2,lastname2,callname2,age2
    etc

    so can somebody please help me????

    --
    Posted via http://www.ruby-forum.com/.
    Lark Work, Nov 15, 2010
    #1
    1. Advertising

  2. On Mon, Nov 15, 2010 at 9:32 AM, Lark Work <> wrote=
    :
    > hi i new to this forum and i have a problem a made a script containing a
    > load data from a file but i want it to load one line at the time the
    > data from that line must be used but i don't know how i (and maybe if
    > its possible i want to call a line with gets.chomp of something) so can
    > u please help me this is my code :
    >
    > class Class_load
    > =A0def initialize(textfile)
    > =A0 =A0@text =3D IO.readlines(textfile)
    > =A0 =A0@data =3D []
    > =A0 =A0for i in
    > =A0 =A0 =A0@text.each(',') {|s| @data.push(s.delete(","))}
    > =A0 =A0end
    > =A0 =A0show_load_data
    > =A0end
    > =A0def load_data
    > =A0end
    > =A0def show_load_data
    > =A0 =A0puts "Name: #{@data[0]} #{@data[1]}"
    > =A0 =A0puts "Callname: #{@data[2]}"
    > =A0 =A0puts "Age: #{@data[3]}"
    > =A0end
    > end
    >
    > Class_load.new("data.txt")
    >
    >
    > and this is how my data.txt looks like:
    >
    > name,lastname,callname,age
    > name2,lastname2,callname2,age2
    > etc
    >
    > so can somebody please help me????


    Depending on what you want to do with the data after reading it, if
    it's just printing it to the screen I would do something like this:

    irb(main):009:0> File.foreach("data.txt") do |line|
    irb(main):010:1* name,last_name,call_name, age =3D line.chomp.split(",")
    irb(main):011:1> p [name,last_name,call_name, age]
    irb(main):012:1> end
    ["name", "lastname", "callname", "age"]
    ["name2", "lastname2", "callname2", "age2"]


    (substitue line 11 with your printing logic). If you want to store the
    data in memory to process later, you could use an array of Struct or
    something like that, for example:

    Person =3D Struct.new :name,:last_name,:call_name,:age

    class PersonList
    attr_reader :persons

    def initialize file
    @persons =3D []
    File.foreach("data.txt") do |line|
    @persons << Person.new(line.chomp.split(","))
    end
    end
    end

    person_list =3D PersonList.new("data.txt")
    p person_list # do something with person_list.persons

    Jesus.
    Jesús Gabriel y Galán, Nov 15, 2010
    #2
    1. Advertising

  3. Lark Work

    Lark Work Guest

    hi thanks for your reply i going to try it what i want is that is just
    prints it in

    puts "Name: #{@data[0]} #{@data[1]}"
    puts "Callname: #{@data[2]}"
    puts "Age: #{@data[3]}"

    data0 = name
    data1 = lastname
    data2 = callname
    and data3 = age

    and thats what i want to call from the txt file

    --
    Posted via http://www.ruby-forum.com/.
    Lark Work, Nov 15, 2010
    #3
  4. On Mon, Nov 15, 2010 at 10:19 AM, Lark Work <> wrot=
    e:
    > hi thanks for your reply i going to try it what i want is that is just
    > prints it in
    >
    > =A0 =A0puts "Name: #{@data[0]} #{@data[1]}"
    > =A0 =A0puts "Callname: #{@data[2]}"
    > =A0 =A0puts "Age: #{@data[3]}"
    >
    > data0 =3D name
    > data1 =3D lastname
    > data2 =3D callname
    > and data3 =3D age
    >
    > and thats what i want to call from the txt file


    If you just want to print it, I wouldn't bother with classes or
    anything more complex:

    File.foreach("data.txt") do |line|
    name,last_name,call_name, age =3D line.chomp.split(",")
    puts "Name: #{name} #{last_name}"
    puts "Callname: #{call_name}"
    puts "Age #{age}"
    end

    This should suffice.

    Jesus.
    Jesús Gabriel y Galán, Nov 15, 2010
    #4
  5. Lark Work

    Lark Work Guest

    thanks for your help, but i got that already i don't think you quit
    understand me because i want to call just one line of at the time this
    is how data.txt looks like

    hisname,lastname,callname,age
    otherperson,lastname,callname,age
    etc

    i wan't it to work like a a database of datas of persons and i want to
    call one person at the time is that posible???


    thanks,


    lark

    --
    Posted via http://www.ruby-forum.com/.
    Lark Work, Nov 15, 2010
    #5
  6. On Mon, Nov 15, 2010 at 7:47 PM, Lark Work <> wrote:
    > thanks for your help, but i got that already i don't think you quit
    > understand me


    Sorry about that. Let's try to move forward then:

    > because i want to call just one line of at the time this
    > is how data.txt looks like


    I have problems understanding what you mean by "call".

    > hisname,lastname,callname,age
    > otherperson,lastname,callname,age
    > etc
    >
    > i wan't it to work like a a database of datas of persons and i want to
    > call one person at the time is that posible???


    Here I don't understand what you mean by "call" and "at the time". If
    you want it to work like a database of persons, then you would like to
    load the data into a data structure that supports containing the list
    of persons, and querying by some key, for example, the name. Is that
    something that makes sense? I don't have time right now, but I'll come
    back later with an example of what I mean.

    Jesus.

    >
    >
    > thanks,
    >
    >
    > lark
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
    Jesús Gabriel y Galán, Nov 15, 2010
    #6
  7. 2010/11/15 Jes=FAs Gabriel y Gal=E1n <>:
    > On Mon, Nov 15, 2010 at 7:47 PM, Lark Work <> wro=

    te:
    >> thanks for your help, but i got that already i don't think you quit
    >> understand me

    >
    > Sorry about that. Let's try to move forward then:
    >
    >> =A0because i want to call just one line of at the time this
    >> is how data.txt looks like

    >
    > I have problems understanding what you mean by "call".
    >
    >> hisname,lastname,callname,age
    >> otherperson,lastname,callname,age
    >> etc
    >>
    >> i wan't it to work like a a database of datas of persons and i want to
    >> call one person at the time is that posible???

    >
    > Here I don't understand what you mean by "call" and "at the time". If
    > you want it to work like a database of persons, then you would like to
    > load the data into a data structure that supports containing the list
    > of persons, and querying by some key, for example, the name. Is that
    > something that makes sense? I don't have time right now, but I'll come
    > back later with an example of what I mean.


    Got a couple of minutes:

    Person =3D Struct.new :name,:last_name,:call_name,:age

    class PersonList
    attr_reader :persons

    def initialize file
    @persons =3D {}
    File.foreach(file) do |line|
    name, *rest_of_data =3D line.chomp.split(",")
    @persons[name] =3D Person.new(name, *rest_of_data)
    end
    end
    end

    person_list =3D PersonList.new("data2.txt")
    p person_list.persons["name"]

    I tried with this file:

    name,lastname,callname,age
    name2,lastname2,callname2,age2


    and got:

    $ ruby persons.rb
    #<struct Person name=3D"name", last_name=3D"lastname",
    call_name=3D"callname", age=3D"age">


    Jesus.
    Jesús Gabriel y Galán, Nov 15, 2010
    #7
  8. Lark Work

    Lark Work Guest

    he jesus thanks for your help by call i meant, when the scripts start i
    can choose from database when i do that i can type in a name of who i
    wan't to show. i hope i am making my self clear now (i am 15 years old
    and not english so my english isn't so good:p).

    i will try it thanks again i realy apricate it


    lark

    --
    Posted via http://www.ruby-forum.com/.
    Lark Work, Nov 15, 2010
    #8
  9. Lark Work

    Lark Work Guest

    heey if that last script that you maked is what i need?? how do i put it
    toghteter i am trying buts its gives my a lot of errors and like a said
    i am not so old and don't have many experience

    --
    Posted via http://www.ruby-forum.com/.
    Lark Work, Nov 15, 2010
    #9
  10. On Mon, Nov 15, 2010 at 8:58 PM, Lark Work <> wrote:
    > heey if that last script that you maked is what i need?? how do i put it
    > toghteter i am trying buts its gives my a lot of errors and like a said
    > i am not so old and don't have many experience


    What errors did you get? I tried with your sample file and got it working.
    In any case, what is left is to use gets to have the user type a name
    and use that to look in the hash (instead of the fixed "name" I
    wrote).

    Jesus.
    Jesús Gabriel y Galán, Nov 15, 2010
    #10
  11. Lark Work

    Lark Work Guest

    he jezus i'm so sorry but no i'm running of the hook i would really
    apriciete if you could put it into my scipt:

    class_choose is the begining of my script and that is what i call up:

    class Class_choose
    def initialize
    puts "Create new?"
    puts "Yes / No / Load"
    choose
    end
    def choose
    @choose = gets.chomp
    if @choose == "yes"
    puts ""
    Class_new.new
    elsif @choose == "no"
    puts ""
    puts "exit"
    elsif @choose == "load"
    puts ""
    Class_load.new
    else
    puts ""
    puts "error"
    Class_choose.new
    end
    end
    end

    class Class_load
    def initialize
    File.foreach("data.txt") do |line|
    @name,@last_name,@call_name, @age = line.chomp.split(",")
    show_load_data
    end
    end
    def show_load_data
    puts "Name: #{@name} #{@last_name}"
    puts "Callname: #{@call_name}"
    puts "Age: #{@age}"
    end
    end


    thanks lark

    --
    Posted via http://www.ruby-forum.com/.
    Lark Work, Nov 15, 2010
    #11
  12. First I reply to the first post : by moving show_load_data inside the
    loop, you can list the file :

    class Class_load
    def initialize(textfile)
    @text = IO.readlines(textfile)
    @data = []
    for i in
    @text.each(',') {|s| @data.push(s.delete(","))}
    show_load_data # moved inside the loop
    @data = [] # clear array
    end
    end

    def show_load_data
    puts "Name: #{@data[0]} #{@data[1]}"
    puts "Callname: #{@data[2]}"
    puts "Age: #{@data[3]}"
    end
    end

    Class_load.new("data.txt")

    ...>ruby -w show_data1.rb
    Name: name lastname
    Callname: callname
    Age: age
    Name: name2 lastname2
    Callname: callname2
    Age: age2

    ---- or ----

    class Class_load
    def initialize(textfile)
    @text = IO.readlines(textfile)
    for i in
    show_load_data(@text)
    end
    end

    def show_load_data(line)
    @data = []
    line.each(',') {|s| @data.push(s.delete(","))}
    puts "Name: #{@data[0]} #{@data[1]}"
    puts "Callname: #{@data[2]}"
    puts "Age: #{@data[3]}"
    end
    end

    Class_load.new("data.txt")

    --
    Posted via http://www.ruby-forum.com/.
    Bernard Kaiflin, Nov 17, 2010
    #12
  13. Second, some comments to your last post :
    - Class_choose.new, in case of wrong answer, creates a new instance of
    Class_choose (a certain number of bytes of RAM) and recursively calls
    choose (after millions of errors, it will fill in all the available
    memory)
    - new is used to create an object. If you don't use it, as in
    Class_load.new("data.txt"), you can as well define a class method, for
    example Class_load.openOn("data.txt"). A class method is defined with
    def self.aMethod or def aClass.aMethod.
    - a class is rather used to represent something (a bank account, a
    person, a vehicle) than to perform a single action (as in Class_choose,
    Class_new and Class_load). Again, Class_load.new creates an instance of
    Class_load, which you do not use.

    Having said that, con gra tulations for what you have already realized.

    Next comes one possible solution.

    --
    Posted via http://www.ruby-forum.com/.
    Bernard Kaiflin, Nov 17, 2010
    #13
  14. If I have well understood, you want to display one of the names of the
    database, by asking a question at the console. Here is a solution. Note
    that I have little experience with Ruby, and there are plenty of ways to
    do the same thing, especially in Ruby.

    class SomeDataProcessing
    Person = Struct.new :name, :last_name, :call_name, :age

    def self.openOn(p_file)
    # this class method is a trick to hide new
    # could be more stuff here
    new(p_file)
    end

    def initialize(p_file)
    @file = p_file # not necessary if used only once,
    loadData(p_file) instead
    @persons = {} # or Hash.new
    loadData
    end

    def addPerson
    puts "Name of new person ?"
    name = gets.chomp
    # other data
    puts "Age of new person ?"
    age = gets.chomp
    @persons[name] = Person.new(name, # last_name, call_name,
    age)
    end

    def choose
    loop do
    puts "Create new ? ( y(es) / l(ist) / n(ame) / q(uit) )"
    answer = gets.chomp.slice(0,1)

    case answer
    when "y"
    addPerson
    when "l"
    listData
    when "n"
    puts "Name of person ?"
    name = gets.chomp
    showData(name)
    when "q"
    exit
    else
    puts "Please anser y[es], l[ist], n[ame] or q[uit]"
    puts ""
    end
    end
    end

    def listData
    @persons.keys.sort.each {|p| showData(p)}
    end

    def loadData
    File.foreach(@file) do |line|
    name, last_name, call_name, age = line.chomp.split(",")
    @persons[name] = Person.new(name, last_name, call_name, age)
    end
    end

    def showData(p_name)
    person = @persons[p_name]

    if person.nil?
    then
    puts "#{p_name} not in file"
    return
    end

    puts "Name: #{person.name} #{person.last_name}"
    puts "Callname: #{person.call_name}"
    puts "Age: #{person.age}"
    puts
    end
    end # class SomeDataProcessing

    dp = SomeDataProcessing.openOn("data.txt")
    dp.choose

    ...>ruby -w show_data22.rb
    Create new ? ( y(es) / l(ist) / n(ame) / q(uit) )
    y
    Name of new person ?
    xyzzzzzzzzz
    Age of new person ?
    99
    Create new ? ( y(es) / l(ist) / n(ame) / q(uit) )
    l
    Name: name lastname
    Callname: callname
    Age: age

    Name: name2 lastname2
    Callname: callname2
    Age: age2

    Name: xyzzzzzzzzz 99
    Callname:
    Age:

    Create new ? ( y(es) / l(ist) / n(ame) / q(uit) )
    n
    Name of person ?
    name2
    Name: name2 lastname2
    Callname: callname2
    Age: age2

    Create new ? ( y(es) / l(ist) / n(ame) / q(uit) )
    q

    Hope that helps.
    Bernard

    --
    Posted via http://www.ruby-forum.com/.
    Bernard Kaiflin, Nov 17, 2010
    #14
  15. Lark Work

    Arun Sharma Guest

    expected 1 times, but received it 0 times

    I am initial level of rspec.i am getting a problem as

    my action is as follow

    def get_roles_to_customer
    @user_role = RoleRelation.first:)conditions => ["user_id = ? AND
    customer_id = ?", current_user.id, params[:id]])
    if current_user.is_superadmin
    @allow_roles = Role.all:)conditions => ["role_type = ? ",'user'])
    elsif !@user_role.nil? and (@user_role.role.name == 'admin' or
    @user_role.role.name == 'mgt-cust')
    @allow_roles = Role.all:)conditions => ["role_type = ?AND name !=
    ?",'user','superadmin'])
    elsif !@user_role.nil? and @user_role.role.name == 'manager'
    @allow_roles = Role.all:)conditions => ["role_type = ? AND name
    NOT IN ('admin','mgt-cust')",'user'])
    end
    options =""
    for role in @allow_roles
    options += "<option value=#{role.id}>#{role.name.capitalize
    }</option>"
    end
    render :text => options
    end

    My spec file code is

    controller.should_receive:)get_roles_to_customer).with:)id=>1).and_return(true)
    this line of code is creating a error

    '#<CompaniesController:0xb69d621c> expected :get_roles_to_customer with
    ({:id=>1}) once, but received it 0 times'

    Please post the solution of this problem

    --
    Posted via http://www.ruby-forum.com/.
    Arun Sharma, Nov 17, 2010
    #15
  16. Lark Work

    Lark Work Guest

    heey thanks for your help i 'm going to try your last script i hope this
    will solve my problem :p

    --
    Posted via http://www.ruby-forum.com/.
    Lark Work, Nov 17, 2010
    #16
  17. Lark Work

    Lark Work Guest

    heey i tryed it but i have a little problems,
    i get this error : loadData(p_file) instead


    and i have put this at the and of my file dp =
    SomeDataProcessing.openOn("data.txt")
    dp.choose

    --
    Posted via http://www.ruby-forum.com/.
    Lark Work, Nov 17, 2010
    #17
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?UTF-8?B?TWFydGluIFDDtnBwaW5n?=

    How to load a CSV/TXT file in a textarea?

    =?UTF-8?B?TWFydGluIFDDtnBwaW5n?=, Mar 25, 2006, in forum: HTML
    Replies:
    11
    Views:
    14,393
    Toby Inkster
    Mar 25, 2006
  2. KK
    Replies:
    2
    Views:
    550
    Big Brian
    Oct 14, 2003
  3. Sameen
    Replies:
    2
    Views:
    440
    Victor Bazarov
    Aug 29, 2005
  4. Jochen Brenzlinger
    Replies:
    7
    Views:
    5,540
    Roedy Green
    Sep 15, 2011
  5. Venkatesh
    Replies:
    1
    Views:
    120
Loading...

Share This Page