please help load from txt

L

Lark Work

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 (e-mail address removed)
@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????
 
J

Jesús Gabriel y Galán

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 (e-mail address removed)
=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.
 
L

Lark Work

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
 
J

Jesús Gabriel y Galán

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.
 
L

Lark Work

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
 
J

Jesús Gabriel y Galán

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.
 
J

Jesús Gabriel y Galán

2010/11/15 Jes=FAs Gabriel y Gal=E1n said:
Sorry about that. Let's try to move forward then:


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


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.
 
L

Lark Work

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
 
L

Lark Work

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
 
J

Jesús Gabriel y Galán

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.
 
L

Lark Work

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
 
B

Bernard Kaiflin

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 (e-mail address removed)
@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 (e-mail address removed)
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")
 
B

Bernard Kaiflin

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.
 
B

Bernard Kaiflin

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
 
A

Arun Sharma

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
 
L

Lark Work

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

Lark Work

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top