Please help load from txt

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 [email protected]
@text[i].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???

On Mon, Nov 15, 2010 at 9:32 AM, Lark W. [email protected]
wrote:

for i in [email protected]
end

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 = 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 = Struct.new :name,:last_name,:call_name,:age

class PersonList
attr_reader :persons

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

person_list = PersonList.new(“data.txt”)
p person_list # do something with person_list.persons

Jesus.

hi thanks for your reply thats what i needed but is it possible to call
just one line up by using a gets.chomp for the name and the lastname ???
thanks

i used your first script

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

On Mon, Nov 15, 2010 at 10:19 AM, Lark W. [email protected]
wrote:

and data3 = 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 = line.chomp.split(“,”)
puts “Name: #{name} #{last_name}”
puts “Callname: #{call_name}”
puts “Age #{age}”
end

This should suffice.

Jesus.

2010/11/15 Jess Gabriel y Galn [email protected]:

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 = Struct.new :name,:last_name,:call_name,:age

class PersonList
attr_reader :persons

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

person_list = 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=“name”, last_name=“lastname”,
call_name=“callname”, age=“age”>

Jesus.

On Mon, Nov 15, 2010 at 7:47 PM, Lark W. [email protected]
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.

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

On Mon, Nov 15, 2010 at 8:58 PM, Lark W. [email protected]
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.

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

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

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 [email protected]
@text[i].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 [email protected]
show_load_data(@text[i])
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”)

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.

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

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

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

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 += “#{role.name.capitalize
}”
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

never mind it works thank you very much