On Mon, Nov 15, 2010 at 9:32 AM, Lark Work <lars_werkman / hotmail.com> 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 0... / text.size
> =A0 =A0 =A0@text[i].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.