# Better coding using ruby

**URL:** <https://www.ruby-forum.com/t/better-coding-using-ruby/95429>\
**Category:** Ruby\
**Created:** [May 31, 2006, 10:36pm UTC](https://www.ruby-forum.com/t/better-coding-using-ruby/95429 "2006-05-31T22:36:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul\_DSKraus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed655f/32.png) [@Paul\_DSKraus](https://www.ruby-forum.com/u/Paul_DSKraus)\
**Post date:** [May 31, 2006, 10:36pm UTC](https://www.ruby-forum.com/t/better-coding-using-ruby/95429/1 "2006-05-31T22:36:40Z")

</div>

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 !! 🙂

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

---

<div class="post-metadata">

**Author:** ![Paul\_DSKraus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed655f/32.png) [@Paul\_DSKraus](https://www.ruby-forum.com/u/Paul_DSKraus)\
**Post date:** [May 31, 2006, 10:58pm UTC](https://www.ruby-forum.com/t/better-coding-using-ruby/95429/2 "2006-05-31T22:58:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Paul\_DSKraus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed655f/32.png) [@Paul\_DSKraus](https://www.ruby-forum.com/u/Paul_DSKraus)\
**Post date:** [May 31, 2006, 11:11pm UTC](https://www.ruby-forum.com/t/better-coding-using-ruby/95429/3 "2006-05-31T23:11:00Z")

</div>

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 !! 🙂
> 
> […]

## 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

---

<div class="post-metadata">

**Author:** ![Paul\_DSKraus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed655f/32.png) [@Paul\_DSKraus](https://www.ruby-forum.com/u/Paul_DSKraus)\
**Post date:** [May 31, 2006, 11:20pm UTC](https://www.ruby-forum.com/t/better-coding-using-ruby/95429/4 "2006-05-31T23:20:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Paul\_DSKraus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed655f/32.png) [@Paul\_DSKraus](https://www.ruby-forum.com/u/Paul_DSKraus)\
**Post date:** [June 1, 2006, 1:44am UTC](https://www.ruby-forum.com/t/better-coding-using-ruby/95429/5 "2006-06-01T01:44:24Z")

</div>

On 5/31/06, Paul D. Kraus [removed\_email\_address@domain.invalid](mailto: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
