Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > please help load from txt

Reply
Thread Tools

please help load from txt

 
 
Lark Work
Guest
Posts: n/a
 
      11-15-2010
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/.

 
Reply With Quote
 
 
 
 
Bernard Kaiflin
Guest
Posts: n/a
 
      11-17-2010
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 Removed)
@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 (E-Mail Removed)
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")

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Bernard Kaiflin
Guest
Posts: n/a
 
      11-17-2010
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/.

 
Reply With Quote
 
Bernard Kaiflin
Guest
Posts: n/a
 
      11-17-2010
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/.

 
Reply With Quote
 
Arun Sharma
Guest
Posts: n/a
 
      11-17-2010
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/.

 
Reply With Quote
 
Lark Work
Guest
Posts: n/a
 
      11-17-2010
heey thanks for your help i 'm going to try your last script i hope this
will solve my problem

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Lark Work
Guest
Posts: n/a
 
      11-17-2010
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/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
counting how often the same word appears in a txt file...But my codeonly prints the last line entry in the txt file dgcosgrave@gmail.com Python 8 12-19-2012 06:29 PM
Diff. between FileWriter("f.txt") and OutputStreamWriter(new FileOutputStream("f.txt")) ? Jochen Brenzlinger Java 7 09-15-2011 01:23 AM
How to load a CSV/TXT file in a textarea? =?UTF-8?B?TWFydGluIFDDtnBwaW5n?= HTML 11 03-25-2006 08:47 PM
Opening a txt file to view ( i.e. readme.txt) Sameen C++ 2 08-29-2005 03:14 PM
please help... ...me learn C++ please please please :) KK C++ 2 10-14-2003 02:08 PM



Advertisments