Forum: Ruby Better coding using ruby

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Paul D. Kraus (Guest)
on 2006-06-01 00:36
(Received via mailing list)
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
Kirk H. (Guest)
on 2006-06-01 00:58
(Received via mailing list)
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
Simon Kröger (Guest)
on 2006-06-01 01:11
(Received via mailing list)
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
Simon Kröger (Guest)
on 2006-06-01 01:20
(Received via mailing list)
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
Bill G. (Guest)
on 2006-06-01 03:44
(Received via mailing list)
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
This topic is locked and can not be replied to.