Better coding using ruby


#1

Below is a quick attempt at doing some things in ruby that I have been
doing
in perl.
Question 1 how can I save and then reuse the functions that are defined
in
other applicaitons.
Question 2 how can I rewrite some this in a more ruby way

Itterating over an array and then having to have a counter variable to
make
“in-place” changes seems a little overkill and I am pretty certian there
is
probaly a better way.

I have a lot of pipe delimented text files that I work with that contain
some standard thing that always get converted. Dates and Numbers to name
a
few.

Thanks for you time !! :slight_smile:

def splitpipe( string )
count = 0
record = string.split(’|’)
record.each do |element|
element = element.strip
element = formatnum(element) if element =~ /\d?.\d+/
element = convertdate(element) if element =~ /^\d\d/\d\d/\d\d$/
record[ count ] = element
count += 1
end
end

def formatnum(num)
num = num.to_f
end

def convertdate(date,type=0)
datearray = date.split(’/’)
if datearray[2].to_i > 90
datearray[2] = “19” + datearray[2]
else
datearray[2] = “20” + datearray[2]
end
if type == ‘human’
date = datearray.join("-")
elsif type == ‘array’
datearray
else
date = datearray[2] + datearray[0] + datearray[1]
end
end

salesfile = File.new( FILENAME, ‘r’ )
worksign = 1
objects = Array.new
salesfile.each_line do |line|
record = splitpipe( line.gsub(/^H:/,’’) )

if record[1] == ‘C’ or record[2] == ‘Y’
worksign = -1
record[9] = record[10] if record[1] == ‘C’ #Set qty shipped if
credit
work around
else
worksign = 1
end
record[6] = record[6] * worksign
record[7] = record[7] * worksign
record[9] = record[9] * worksign
record.each { |e| puts e }
exit
end
salesfile.close


#2

On Wednesday 31 May 2006 2:33 pm, Paul D. Kraus wrote:

end
def splitpipe (string)
string.split(’|’).collect do |element|
element = element.strip
element = formatnum(element) if element =~ /\d?.\d+/
element = convertdate(element) if element =~ /^\d\d/\d\d/\d\d$/
end
end


#3

Paul D. Kraus schrieb:

I have a lot of pipe delimented text files that I work with that contain
some standard thing that always get converted. Dates and Numbers to name a
few.

Thanks for you time !! :slight_smile:

[…]

this should do the same:

def splitpipe string
string.split(’|’).map do |element|
element.strip!
case element
when /\d?.\d+/ then element.to_f
when /^\d\d/\d\d/\d\d$/ then element.split(’/’).values_at(2, 0,
1).join
else element
end
end
end

IO.foreach(FILENAME) do |line|
record = splitpipe(line.gsub(/^H:/,’’))

if record[1] == ‘C’ or record[2] == ‘Y’
record[9] = record[10] if record[1] == ‘C’
work around

record[6] *= -1
record[7] *= -1
record[9] *= -1

end
puts record
exit
end

As i have no input data i’m not 100% sure, also what does work around
do?

I would suggest using a struct instead of the ‘not so speaking’
record[6] etc.

cheers

Simon


#4

Simon Kröger schrieb:

probaly a better way.
[…]
sorry i missed that part:

if datearray[2].to_i > 90
datearray[2] = “19” + datearray[2]
else
datearray[2] = “20” + datearray[2]
end

well one should probably use Date#strptime anyway…

cheers

Simon


#5

On 5/31/06, Paul D. Kraus removed_email_address@domain.invalid wrote:

Itterating over an array and then having to have a counter variable to make
“in-place” changes seems a little overkill and I am pretty certian there is
probaly a better way.

%w[a b c d e].each_with_index do |val,idx|
puts “#{idx} = #{val}”
end

0 = a
1 = b
2 = c
3 = d
4 = e